Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

103
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda