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

418
Visualizações
Use for loop to iterate from 0 to 100 and print the sum of all evens and the sum of all odds with JavaScript Loops

I'm trying to answer for this question: "Use for loop to iterate from 0 to 100 and print the sum of all evens and the sum of all odds."

and my code is:


numbersE = []
numbersO = []
let sumE = 0
let sumO = 0


for (i=0;i<=100;i++) {

    if(i % 2 == 0){
        numbersE.push(i)
     }

    else {
        numbersO.push(i)
        
    }
    
    sumE += numbersE[i]
    sumO += numbersO[i]
}

console.log(sumE, sumO)

Nan Nan

where is the my mistake ?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

let sumE = 0
let sumO = 0

// You could technically start at 1 here
for (let i = 0; i <= 100; i++) {
  // Just add the numbers without using arrays
  if (i % 2 == 0) {
    sumE += i
  } else {
    sumO += i
  }
}

console.log(sumE, sumO)

about 4 years ago · Juan Pablo Isaza Relatório

0

  1. Let's start with the loop. At i=0, if statement is triggered and 0 is pushed to the array numbersE and hence sumE = 0. But the array numbersO is empty because the for loop didn't push any values to numbersO. Therefore at i=0, sumO returns NaN.

  2. Similarly when i>0, you are trying to access the index of arrays numbersE, numbersO which does not exist. For example, at i=10, 10%2 equals 0, hence 10 is pushed to the array numbersE. And your code adds an element from the array numbersE with index 10 to the variable sumE and stores it in sumE. But the problem here is that the array numbersE doesn't have an element at index 10. Hence it returns NaN.

This can be solved by:

let i;
let sumE = 0;
let sumO = 0;

for (i = 0; i <= 100; i++) {
  i % 2 === 0 ? (sumE += i) : (sumO += 1);
}

console.log(sumE, sumO);
about 4 years ago · Juan Pablo Isaza Relatório

0

let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100];
let even = 0;
let odd = 0;
for(let i = 0; i < arr.length; i++) {
if(i % 2 === 0) {
even += arr[i];
} else {
odd += arr[i];
}
} 
console.log([even, odd]);   
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