Using while loop

Hi,please I don’t know why the first code isn’t working and the second works.Please your response is important

let num;
while (num >= 100) {
  num = Number(prompt(`You can only enter numbers greater than 100`));
}

let num;
while (!(num <= 100)) {
  num = Number(prompt(`You can only enter numbers greater than 100`));
}

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

ok,I get that,thank you

Because while evaluates to true.

num is undefined.
since num is undefined, num <= 100 is false. You have negate sign infront. Which evaluates to true so your while loop runs.

first while loop won’t run because undefined can’t be equal or greater than 100 so it’s false.

ok do you mean undefined can be less than or equal to 100 but cannot be greater than or equal to 100

No. They are both false.

You 2nd while loop runs because you have ! infront in the condition, which changes false to true.

1 Like