-1

I'm trying to keep the value for each answer in between 2 values. I don't know if this is something I can do or if I'm not doing it right (I clearly am doing it wrong).

I wouldn't want to use an if statement inside an if statement.

Is this something doable or is there a different way of doing the same thing or just a big 'no'? Thanks.

let timeNow = prompt("What time is it?");

if (timeNow >= 0 && < 12) {
    alert("good morning");
} else if (timeNow > 12 && < 18) {
    alert("good afternoon");
} else it (timeNow >18 && < 24) {
    alert("good evening");
}
Langlois.dev
  • 1,951
  • 2
  • 12
  • 15
SaBah
  • 13
  • 5
  • To check for whether a variable `x` is in range `[a, b]` is to write a condition like this: `a <= x && x <= b` – pavi2410 Jan 15 '23 at 17:54

1 Answers1

0

You have to add the variable to both conditions you want to check.

So if (timeNow >= 0 && < 12) {...} becomes if (timeNow >= 0 && timeNow < 12) {...}. Here's an updated snippet of the code you provided:

let timeNow = prompt("What time is it?");

if (timeNow >= 0 && timeNow < 12) {
    alert("good morning");
} else if (timeNow > 12 && timeNow < 18) {
    alert("good afternoon");
} else if (timeNow > 18 && timeNow < 24) {
    alert("good evening");
}

Please note that if timeNow is equal to 12, 18 or 24 none of the conditions would be met. I kept it like that for you as that's what you had originally, but that may not be what you want. You also have a typo the last else it should be else if.

Langlois.dev
  • 1,951
  • 2
  • 12
  • 15
  • 1
    Thank you. Yeah I added >= 12 and >=18, that way it meets the parameters. I don't expect to have more than 24 hours a day so I don't expect a number above that. – SaBah Jan 15 '23 at 18:53