Sleep Debt Calculator JS

I am working on the Sleep Debt Calculator. I’ve worked out everything that is wrong with it besides one thing, and I need your help! On the last step, you are supposed to call calculateSleepDebt() to the console. My code works but prints “You got NaN hour(s) less sleep than you needed this week, you should get some rest”. I don’t know what the problem is! CODE BELOW:

const getSleepHours = (day) => {
day = day.toLowerCase();
switch (day) {
case “Monday”:
return 8;
break;
case “Tuesday”:
return 6;
break;
case “Wednesday”:
return 9;
break;
case “Thursday”:
return 7;
break;
case “Friday”:
return 5;
break;
case “Saturday”:
return 3;
break;
case “Sunday”:
return 11;
break;
default:
return “Please enter a valid day”;
}
};

const getActualSleepHours = () => {
return (
getSleepHours(“Monday”) +
getSleepHours(“Tuesday”) +
getSleepHours(“Wednesday”) +
getSleepHours(“Thursday”) +
getSleepHours(“Friday”) +
getSleepHours(“Saturday”) +
getSleepHours(“Sunday”)
);
};

const getIdealSleepHours = () => {
let idealHours = 9;
return idealHours * 7;
};

const calculateSleepDebt = () => {
const actualSleepHours = getActualSleepHours();
const idealSleepHours = getIdealSleepHours();
if (actualSleepHours === idealSleepHours) {
console.log(“You got” + actualSleepHours + “the perfect amount of sleep”);
} else if (actualSleepHours > idealSleepHours) {
console.log(“You got” + (actualSleepHours - idealSleepHours) + " hour(s) more sleep than you needed this week, you got enough sleep!");
} else {
console.log(“You got " + (idealSleepHours - actualSleepHours) + " hour(s) less sleep than you needed this week, you should get some rest”);
}
};
calculateSleepDebt();

// You wrote:
day = day.toLowerCase();

but your cases are in title case i.e. "Monday", "Tuesday", ...

None of the cases will match the switch expression ("monday", "tuesday", ... ) and the default string "Please enter a valid day" will be returned.
In getActualSleepHours(), multiple copies of the string are concatenated and returned as a single string, "Please enter a valid dayPlease enter a valid day..."
In calculateSleepDebt(), the if and else-if statements will evaluate as false and the else block will be executed. In the console statement, you end up trying to subtract a string from a number which results in NaN i.e. Not-A-Number .

Either remove the lower case statement OR keep the lower case statement but rewrite the cases in lowercase i.e.

day = day.toLowerCase();
switch (day) {
    case “monday”:
    ...
1 Like

Thank you so much! I will try tonight! I appreciate your help! Thanks for making forums better.

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.