Home > database >  How do I loop through this number array to find if a number is <25 or >= 25?
How do I loop through this number array to find if a number is <25 or >= 25?

Time:09-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)

CodePudding user response:

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)

CodePudding user response:

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)
}

CodePudding user response:

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.

numArray = [4, 2, 5, 10, 5, 3, 7, 22, 3, 70, 37];

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

let message = (numArray > 25) ? "Big Number!" : "Small number"

console.log(message)

}

CodePudding user response:

Your for loop is not wrapped in curly braces. This is what it should look like:

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)
}

CodePudding user response:

Couple of issues:

  • Your for loop is not wrapped in curly brackets
  • You mentioned being new at this, and I think avoiding the ternary operator would help make this more readable.
let numArray = [4, 2, 5, 10, 5, 3, 7, 22, 3, 70, 37];
for (let i = 0; i < numArray.length; i  ) {
  if (numArray[i] > 25) {
      message = "Big Number!"
  } else {
      message = "Small number"
  }
  console.log(message)
}

If you really want to stick with the current structure, try this:

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