Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

102
Views
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;
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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++) {

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!