【发布时间】:2022-01-07 01:06:00
【问题描述】:
我是 java 脚本的新手,希望能得到一些帮助。我环顾谷歌,但我找不到任何与我的问题相匹配的东西,尽管我确信它在某处被覆盖。如果有的话,我提前道歉。我的问题是使用 for 循环访问数组的元素。如果我使用特定数字,我可以访问元素,例如test[1].property 但不是for(var i=0; i<1;i++){test[i].property}。后者在访问未定义的属性时返回错误。
function generateTestCases() {
const body = document.getElementsByTagName("body")[0];
const tbl = document.createElement("table");
const tblBody = document.createElement("tbody");
//array of test cases to print to the page
let testCases = [
createNewTestCase(5,47),
createNewTestCase(3, 0o0),
createNewTestCase(7,29),
createNewTestCase(5,30),
createNewTestCase(5,45),
createNewTestCase(4,15),
createNewTestCase(6,35),
createNewTestCase(3,30),
createNewTestCase(10,57),
createNewTestCase(12,45)
]
for (var i = 0; i < 12; i++) {
let row = document.createElement("tr");
//hours column of table
let cell = document.createElement("td");
let hours = testCases[i].hours;
let cellText = document.createTextNode(`${hours}:`);
cell.appendChild(cellText);
row.appendChild(cell);
//minutes column of table
cell = document.createElement("td");
let minutes = testCases[i].minutes;
cellText = document.createTextNode(`${minutes}`);
cell.appendChild(cellText);
row.appendChild(cell);
//output column of table
cell = document.createElement("td");
let output = testCases[i].output;
cellText = document.createTextNode(`${output}`);
cell.appendChild(cellText);
row.appendChild(cell);
//add finished row to table
tblBody.appendChild(row);
}
tbl.appendChild(tblBody);
body.appendChild(tbl);
tbl.setAttribute("border", "1");
}
function createNewTestCase(hoursTest, minutesTest){
const testCase = {
hours: hoursTest,
minutes: minutesTest,
output: convertTimeToWords(hoursTest, minutesTest)
};
return testCase;
}
【问题讨论】:
标签: javascript arrays javascript-objects