-2

Please could someone tell me where I'm going wrong here, I know it's something simple I'm missing so forgive my ignorance but I can't find the error.

let day = "Monday"

function alarmTime(day) {
  if (day == "Saturday") {
    return "Set alarm for 8:00am"
  } else if (day == "Friday") {
    return "Set alarm for 5:35am"
  } else if (day == "Sunday" || "Thursday") {
    return "Set alarm for 4:50am"
  } else {
    return "Set alarm for 7:00am"
  }
}

console.log(alarmTime(day));

if day is Mon, Tue, Wed then it returns 4:50am??? Why?? It should reach the final else statement and return 7:00am ‍♀️

  • 3
    "[How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)" – Uwe Keim Jan 12 '23 at 10:45
  • Use linters like [ESLint](//eslint.org/play) or [JSHint](//jshint.com) to find problems with your code immediately. Relevant linter warning: [_“Unexpected constant condition”_](//eslint.org/docs/latest/rules/no-constant-condition). – Sebastian Simon Jan 12 '23 at 10:54

5 Answers5

2

Because day == "Sunday" || "Thursday" evaluates to Thursday. Since if ("Thursday") evaluates to true, it always enters in this block.

What you want to have is day == "Sunday" || day == "Thursday"

Aid Hadzic
  • 578
  • 3
  • 10
0

The or operator, ||, accepts two conditions, not two possible values for a variable

ie: foo == "hello" || foo == "hi"

not foo == "hello" || "hi" as that just evaluates "hi" which returns true like all strings

TorNato
  • 305
  • 2
  • 12
0

Below is the updated fuction. It returns 7:00am

function alarmTime(day) {
  if (day == "Saturday") {
    return "Set alarm for 8:00am"
  } else if (day == "Friday") {
    return "Set alarm for 5:35am"
  } else if (day == "Sunday" || day == "Thursday") {
    return "Set alarm for 4:50am"
  } else {
    return "Set alarm for 7:00am"
  }
}
Dave
  • 4,376
  • 3
  • 24
  • 37
0

The issue is that the third else if statement does not have a condition for the day to be "Sunday" or "Thursday". Instead, it has a condition for day to be "Sunday" or the boolean value true, which will always evaluate to true. This causes the code to always return "Set alarm for 4:50am".

To fix this, you can change the third else if statement to:

else if (day == "Sunday" || day == "Thursday")

Example with fixed code:

let day = "Monday"

function alarmTime(day) {
  if (day == "Saturday") {
    return "Set alarm for 8:00am"
  } else if (day == "Friday") {
    return "Set alarm for 5:35am"
  } else if (day == "Sunday" || day == "Thursday") {
    return "Set alarm for 4:50am"
  } else {
    return "Set alarm for 7:00am"
  }
}

console.log(alarmTime(day));
Filip Huhta
  • 2,043
  • 7
  • 25
0
day == "Sunday" || "Thursday"

This says, if the day is "Sunday" return true, else return "Thursday". "Thursday" is a string, so JavaScript converts it to true. See here

Look at the || operator like this (day == "Sunday") || ("Thursday")

You want to say:

day == "Sunday" || day =="Thursday"

Bonus tip: use === instead of == This is the best way to put it:

day === "Sunday" || day ==="Thursday"
Jash1395
  • 228
  • 1
  • 6