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

369
Vistas
How do I get the calculation of the sum of the items in an array with a for loop?

So I am a bit lost on this one, so what I did was create the for loop that shows their index, for example: 0 1 2 3 4 5 6 7 8 9.

let tasks =["swim", "study", "eat", "break", "sleep", "Cook", "Clean", "watch TV", "Train", "Code"]
let x;


for(x of tasks)
{
    document.getElementById("answers").innerHTML += x + " ";
    console.log(x);
}

so the above code I used the for of loop which gets index of each item. What I am trying to do is how do I get the add each index to each other, so it should be 1 + 2 + 3...etc.

//Activity 2 - Edit the loop created above, to now calculate the sum of all the items leading up to 10. e.g. 1 + 2 + 3 = 6
//console.log your output
//Add your code below

let index;
let sum;


for(x = 0; x < tasks.length; x++)
{
    index = document.getElementById("sumOftasks").innerHTML += x + " ";
}
over 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Your code will return values of array and when you sum it it Will give you result NaN

You can get value from array, Find index and Sum it.

    for(x of tasks)
    {
                
     let index = tasks.indexOf(x);
 document.getElementById("answers").innerHTML += index + " ";
    }

It will help.

over 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
Asistente virtual