-1

I have a simple a HTML document with a script tag, I'm messing around with a try catch and can't get the while statement in the catch block to work. The try and catch simply runs as if the while block isn't there.

try {
  let age = prompt("age?")
  if (age <= 0 || age >= 120) {
    throw new Error("Something Happened!")
  }
} catch (e) {
  let state = true;
  while (state) {
    age = prompt("age?");
    if (age > 0 || age < 120) {
      state = false;
    }
  }
}
Brandon
  • 839
  • 1
  • 7
  • 11
  • 1
    `if(age>0|| age<120){` All numbers are either greater than `0` OR less than `120`. You need `&&` instead. –  Jan 27 '17 at 00:13

1 Answers1

1

age>0|| age<120 is always true, so you always set state to false and quit the loop after one iteration.

You should use && instead of || for that condition, then it will only be true for numbers that are both greater than 0 and less than 120.

Paul
  • 130,653
  • 24
  • 259
  • 248