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

342
Visualizações
How do I loop through this number array to find if a number is <25 or >= 25?

Apologies in advance if this is too simple. It should say "Small number" nine times and "Big Number!" two times. Instead it hangs on the console.log. Please tell me what I'm doing wrong.

let numArray = [4, 2, 5, 10, 5, 3, 7, 22, 3, 70, 37];
    for (let i = 0; i < numArray.length; i++);
      let message = (numArray[i] > 25)? "Big Number!":
                    (numArray[i] <= 25)? "Small number"
                   console.log(message)
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Your code refers only to numArray as a whole, never to the i'th entry in numArray. You probably want to compare to numArray[i] or similar, rather than just numArray.

(also, may want to tag what language you're programming in, in case there are language-specific issues, but I'm pretty sure that issue will be a bug in any language)

about 4 years ago · Juan Pablo Isaza Relatório

0

Assuming that this is javascript (or typescript perhaps) I see a few problems here. First of the semicolon at the end of the for statement is ending the loop altogether without being executed as a code block you should remove it if you want to iterate through the array numArray

for (let i = 0; i < numArray.length; i++)

Second, after you remove the semicolon at the end of that line if you intended to execute both the ternary operation (it didn't work for me on javascript if this is another language, it would be great if you can update your question and clarify that) and output to the console, you should enclose both lines in brackets, like so:

{
    let message = (numArray[i] > 25) ? "Big Number!" : "Small number";
    console.log(message)
}

And of course, you are comparing the whole array not just an element, so you need to get an item using the index

   numArray[i] > 25 ...

With those corrections, I can get the output that you are expecting (Small number nine times and Big number two times)


for the sake of clarity I've added (What i see) as the fixed code (in javascript/typescript) right below:

let numArray = [4, 2, 5, 10, 5, 3, 7, 22, 3, 70, 37];
for (let i = 0; i < numArray.length; i++) {
  let message = (numArray[i] > 25) ? "Big Number!" : "Small number";
  console.log(message)
}

about 4 years ago · Juan Pablo Isaza Relatório

0

You forgot to wrap your logic inside the for loop you actually forgot the { } and no need to specify the second condition just put else.

const numArray = [4, 2, 5, 10, 5, 3, 7, 22, 3, 70, 37];
for (let i = 0; i < numArray.length; i++) {
  let message = numArray[i] > 25 ? "Big Number!" : "Small number"
  console.log(message)
}

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