jmaslak
jmaslak

Reputation: 25

trouble accessing elements of an array with for loop

I'm new to java script and would appreciate some help. I looked around google but I couldn't find anything that matched my problem although I am sure its been covered somewhere. I apologize in advance if it has. My problem is with accessing the elements of an array using a for loop. I am able to access the elements if I use a specific number e.g. test[1].property but not for(var i=0; i<1;i++){test[i].property}. The latter returns an error for accessing a property of undefined.

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;
}

Upvotes: 1

Views: 45

Answers (1)

GenericUser
GenericUser

Reputation: 3230

It looks like your range is too high in your for loop. You have it running from 0-12, which is 13 entries. However, in your testCases array there are only 10 entries:

    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)
    ]

Try reducing the range from 12 to 9 at the beginning of your loop

for (var i = 0; i < 9; i++) {

Upvotes: 1

Related Questions