Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Simple if else function not returning as expected

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 🤦‍♀️ 🙏

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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"

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading