Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

330
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!