Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

299
Visualizações
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 + " ";
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

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.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda