Stuck on "Sleep Dept Calculator" project

I didn’t want to type [getSleepHours(‘Monday’) + getSleepHours(‘Tuesday’) + getSleepHours(‘Wednesday’) + …] over and over again so I decided to do this (see code attached below).
After calling the code using console.log(getSleepHours(0)) it was initially showing the value 8 but now after a few attempts it’s showing “undefined”!!

This is my code,
let getSleepHours = day =>{
if (day.isLetter==true)day= day.toLowerCase()
switch (day){
case 0||‘monday’: return 8
break;
case 1||‘tuesday’: return 2
break;
case 2||‘wednesday’: return 6
break;
case 3||‘thursday’:return 8
break;
case 4||‘friday’: return 8
break;
case 5||‘saturday’: return 8
break;
case 6||‘sunday’: return 8
break;
}
}
console.log(getSleepHours(0))
/let getActualSleepHours = ()=>{
let Totalsleep=0;
for(let i=0;i<7;i++){
Totalsleep+=getSleepHours(i)
}
return Totalsleep
}
console.log(getActualSleepHours())
/

what change all of a sudden?
If I run the code without case 0 that is for(let i=1;i<7;i++) the console.log(getActualSleepHours()) shows 40 which is accurate!

But with case 0 that is for(let i=0;i<7;i++) the console.log(getActualSleepHours()) shows NaN!

link to project - https://www.codecademy.com/courses/introduction-to-javascript/projects/sleep-debt-calculator

The 0||'monday' and case 1||'tuesday' does not do what you think it does …
it will just give you the first truthy value.
and non-zero numbers are truthy (work like true), and non-empty strings are also truthy

To fix that, you could do separate cases:

  case 0:
    return 8;
  case 'monday':
    return 8;

or using fall-through:

  case 0:
  case 'monday':
    return 8;

Another alternative is to use if and else-if instead of using switch and cases

  if (day === 0 || day === 'monday') {
    return 8;
  }
  else if (day === 1 || day === 'tuesday') {
    return 2;
  }
2 Likes

Thank you! It was really helpful. But I used a different method. I think because JavaScript takes 0 as a falsey value thats why it was showing “Undefined” and “NaN” as the sum. I changed the the case 0 to 1, i.e, case 1||‘monday’ instead of case 0||‘monday’ and the loop for(let i=1; i<=7;i++) instead of for(let i=0;i<7;i++) and this time it worked perfectly! The output was 8 and 48!

Seeing an expected output for a test shouldn’t lull us into thinking that our program is working perfectly. Thorough testing should be done to see if there are any issues.

You should have a look at the post from @janbazant1107978602 above. If you wish to use the switch approach, then perhaps the fall-through would be a suitable solution.

I am not a subscriber and don’t know the specs of the project. So, I don’t know your motivation for having the cases check for both number and string values for day.

If day is supposed to be a number only, then you can omit the || and strings from the cases. In that situation, having the cases go from 0-6 or 1-7, both will work.

But if day can be a number or a string, then the code you posted as well as your revised code will not work.
Your original code would work for numbers 1-6 and “monday”. But for the number 0 as well as “tuesday”, “wednesday”, … ,“sunday”, no case will match and you will get undefined.
Your revised code will work for numbers 1-7 but will not work for the strings “monday” through “sunday”. You can verify this by testing your program with different arguments.

As noted by janbazant1107978602, the problem lies in the use of the OR || operator.
Your intention in a case like 0 || "monday" is to check whether “day is 0” or “day is ‘monday’”.
But, that is not how it is understood by Javascript. It will evaluate the expression and come up with a single value. Then it will compare that single value against the value of day to determine whether the case is applicable. You can experiment with different operands and see what happens:

//  Console statement                   Output
console.log(0 || "monday");        //  "monday"
console.log(1 || "monday");        //     1
console.log(2 || "tuesday");        //     2

// Switch statement
case 1 || "monday":  /*will just simplify to*/ case 1:  
// day will not be checked against 'monday'. 
// If day is 'monday', then none of the cases will match resulting in undefined

case 0 || "monday":  /*will just simplify to*/ case "monday":  
// day will not be checked against 0 
// If day is 0, then none of the cases will match resulting in undefined

The documentation mentions about the OR || operator “…if this operator is used with non-Boolean values, it will return a non-Boolean value.” You may also want to look at the examples in the documentation.

2 Likes

Thank you @janbazant1107978602 @mtrtmk ! You were absolutely correct! I went back and ran the console.log(getSleepHours(‘monday’)) code the output was undefined. It appears we can’t use OR || Operator like the way I did in switch case.

1 Like