您好我有一项技术测试,要求我将180个字符分成6组,然后将它们分成6个表。
由于我几天前来到这里的帮助,我已经将角色拆分了,但是我意识到我需要再次拆分新形成的阵列;一旦创建了表,这6个组就会将自己登录到6个表中第一个的顶行。
我需要将这些组成对记录到9列x 3行表中,我似乎无法让它们工作。这是我的代码:
<script>
var ticketString ="011722475204365360702637497481233455758302154058881928446789061241507324334876840738576186051132437816395663800818206590104559628214294664710935667287132130687703253151692742547985".match(/.{1,2}/g);
function loadTickets () {
var ticket = [];
for (var i = 0; i < ticketString.length / 15; i++) {
ticket[i] = ticketString.slice(i * 15, (i + 1) * 15).sort();
var table = document.createElement("TABLE")
table.border='1'
table.setAttribute("id","ticketTable" + i)
var tableBody = document.createElement("TBODY")
tableBody.setAttribute("id","ticketBody")
table.appendChild(tableBody);
for (b=0;b<=2;b++) {
var row = document.createElement("TR")
row.setAttribute("id", "ticketRow" + b)
tableBody.appendChild(row)
for (c=0; c<=8;c++) {
var cell = document.createElement("TD")
cell.setAttribute("id","ticket-number" + c)
cell.bgColor = 'red';
row.appendChild(cell)
document.body.appendChild(table)
document.getElementById('ticket-number' + i).innerHTML = ticket[i];
}
}
}
console.log(tableBody);
console.log(row);
console.log(cell);
};
</script>
答案 0 :(得分:1)
我不确定你是如何确定空盒子的,但这是6张表,每行有3行和5个数字,就像你的照片一样。基本上它执行以下操作:
在视觉上你可以把它想象成堆叠多维数组。
var ticketString = "011722475204365360702637497481233455758302154058881928446789061241507324334876840738576186051132437816395663800818206590104559628214294664710935667287132130687703253151692742547985".match(/.{1,2}/g);
function loadTickets() {
var ticketTables = [];
var ticketGroups = [];
var numberOfNumbers = 5; // per row
var inc = 0;
for (var i = 0; i < ticketString.length / numberOfNumbers; i++) {
var start = i * numberOfNumbers;
var end = start + numberOfNumbers;
ticketGroups[i] = ticketString.slice(start, end);
ticketTables[inc] = ticketTables[inc] || [];
ticketTables[inc].push(ticketGroups[i]);
if ((i + 1) % 3 === 0) {
inc += 1;
}
}
for (var i = 0, max = ticketTables.length; i < max; i += 1) {
var table = ticketTables[i];
for (var j = 0, jmax = table.length; j < jmax; j += 1) {
var row = table[j];
var newRow = [];
for (var k = 0, kmax = 9; k < kmax; k += 1) {
newRow[k] = '';
for (var l = 0, lmax = row.length; l < lmax; l += 1) {
var number = parseInt(row[l]);
if (number >= (k) * 10 && number < (k + 1) * 10) {
newRow[k] = number;
}
if (number >= (kmax * 10) && k === kmax - 1) {
newRow[k] = number
}
}
}
ticketTables[i][j] = newRow;
}
}
console.log(ticketTables);
for (var i = 0, max = ticketTables.length; i < max; i += 1) {
var table = ticketTables[i];
var $table = document.createElement('table');
for (var j = 0, jmax = ticketTables[i].length; j < jmax; j += 1) {
var row = table[j];
var $row = document.createElement('tr');
$table.appendChild($row);
for (var k = 0, kmax = row.length; k < kmax; k += 1) {
var cell = row[k];
var $cell = document.createElement('td');
$row.appendChild($cell);
$cell.innerHTML = cell;
}
}
document.body.appendChild($table);
}
};
loadTickets();
input {
width: 100%;
}
table, tr, td {
border: 1px solid grey;
}
table tr td {
min-width: 20px;
}