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

368
Visualizações
What am I doing wrong in this loop? js

I am trying to solve this loop but I can't solve it

You must create a function called loopDePares that receives a number as a parameter and loops from 0 to 100, displaying each loop number on the console. If the number of the iteration, added to the number passed by parameter, is even, it will show in the console “The number x is even”.

what i did was this

function loopDePartes(numero){
  for (let i = 0; i <numero; i++){
    console.log(i+numero % 2 ? i : "x")
  }
}

loopDePartes(10)
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You had one math error, and one comparison error.

i+numero needs to be wrapped in parens so it take precedence over the modulus operator.

Your ternary was checking the truthiness of the output of that math, instead of checking whether it was even or odd.

Both are corrected below:

function loopDePartes(numero){
  for (let i = 0; i < 100; i++){
    console.log((i+numero) % 2 == 1 
      ? i 
      : `The number ${i+numero} is even`
    )
  }
}

loopDePartes(10)

(Actually, the more I reread your question the less I understand what is the expected output; I think this is what you're looking for but I may have misinterpreted what you meant by "display the loop number", and what "x" is supposed to be in "The number x is even".)

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