Issues with my Sleep Debt Calculator code shows 'NaN' for 'calculateSleepDebt'

i have written the code for the Sleep Debt Calculator and it shows ‘NaN’ fo0r the sesult of my “calculateSleepDebt” and i am not sure why! I used an const statement and also i tried a let statement with an equation and it doesnt come up as a number for some reason.

I have also added the code itself, please help me understand why its not showing a number…

const getSleepHours = day => { switch(day){ case "mon": return 8 break; } switch(day){ case "tue": return 8 break; } switch(day){ case "wed": return 6 break; } switch(day){ case "thu": return 8 break; } switch(day){ case "fri": return 8 break; } switch(day){ case "sat": return 8 break; } switch(day){ case "sun": return 8 break; default: return "Error!" } } const getActualSleepHours =() => getSleepHours("mon") + getSleepHours("tue") +getSleepHours("wed") +getSleepHours("thu") getSleepHours("fri") + getSleepHours("sat") +getSleepHours("sun"); console.log(getSleepHours("mon")); console.log(getActualSleepHours()) const getIdealSleepHours = () => { const idealHours =8; return idealHours * 7; }; const calculateSleepDebt = () => { const actualSleepHours =getActualSleepHours; const idealSleepHours = getIdealSleepHours; const calculateSleepDebt = idealSleepHours - actualSleepHours; if (actualSleepHours < idealSleepHours) { console.log ("you got "+ (calculateSleepDebt)+" you need more sleep!"); } else if (actualSleepHours === idealSleepHours) { console.log("you got "+ (calculateSleepDebt)+ " you have a perfect amount of sleep!"); } else if (actualSleepHours > idealSleepHours) { console.log ("you got "+ (calculateSleepDebt)+ " you are sleeping way to much!"); } else { console.log( "error"); } } calculateSleepDebt();

I am not sure why its showing NaN…

Hey @golden-dragon

Thanks for reaching out :+1: This took me some time to figure out :grinning_face_with_smiling_eyes: But it is an easy fix/solution.
At the start of your calculateSleepDebt() function, you create 2 new variables (actualSleepHours & idealSleepHours) but something is wrong with their data. You have just passed another variable into them, which is wrong, because you don’t even have such a variable.

If you look at getActualSleepHours, it is a (function).

A JavaScript function is defined with the function keyword, followed by a name , followed by parentheses () .

Try to figure it out. I hope that I’ve helped in a way. ((:eyes:))

Cheers

1 Like

Hi, thank you so much for your reply, i did find that i missed “()” in the name part of the function:
image
i think that was one error, but i did not understand this part: “something is wrong with their data. You have just passed another variable into them, which is wrong, because you don’t even have such a variable.” and i apologize i am super new to coding :slight_smile: but if you could please elaborate, i would very much appreciate that. and thanks for your reply so far :slight_smile:

1 Like

Hey Hey!

I am new myself, that’s why my explanation wasn’t so good :grinning_face_with_smiling_eyes: I’ll try to elaborate. Inside of your calculateSleepDebt() function, when you create the 2 new variables actualSleepHours & idealSleepHours, you pass the getActualSleepHours & getIdealSleepHours without any (parentheses).

When you look at getActual&IdealSleepHours, you have created them as a function, they are not just another variable. And they won’t be executed until they are called properly, meaning getActualSleepHours(). You can’t call a function without it’s parentheses :man_shrugging:

I don’t want to make it any harder on you, so just pay attention to line 63/64:

const getSleepHours = day => { switch(day){ case "mon": return 8 break; } switch(day){ case "tue": return 8 break; } switch(day){ case "wed": return 6 break; } switch(day){ case "thu": return 8 break; } switch(day){ case "fri": return 8 break; } switch(day){ case "sat": return 8 break; } switch(day){ case "sun": return 8 break; default: return "Error!" } } const getActualSleepHours =() => getSleepHours("mon") + getSleepHours("tue") +getSleepHours("wed") +getSleepHours("thu") getSleepHours("fri") + getSleepHours("sat") +getSleepHours("sun"); console.log(getSleepHours("mon")); console.log(getActualSleepHours()) const getIdealSleepHours = () => { const idealHours =8; return idealHours * 7; }; const calculateSleepDebt = () => { const actualSleepHours =getActualSleepHours(); //YOu need to properly call the function with it's parentheses const idealSleepHours = getIdealSleepHours(); //You need to properly call the function with it's parentheses const calculateSleepDebt = idealSleepHours - actualSleepHours; if (actualSleepHours < idealSleepHours) { console.log ("you got "+ (calculateSleepDebt)+" you need more sleep!"); } else if (actualSleepHours === idealSleepHours) { console.log("you got "+ (calculateSleepDebt)+ " you have a perfect amount of sleep!"); } else if (actualSleepHours > idealSleepHours) { console.log ("you got "+ (calculateSleepDebt)+ " you are sleeping way to much!"); } else { console.log( "error"); } } calculateSleepDebt();
3 Likes

Something to consider when writing a switch: Start with the default.

switch() {

default: // code
}

Now fill in all the cases without their actions. The code will still run and output the default.

function foo(x) {
  switch(x) {
  case 'mon':
  case 'tue':
  case 'wed':
  case 'thu':
  case 'fri':
  case 'sat':
  case 'sun':
  default: return 8
  }
}

Test cases:

console.log(foo(''))       //  8
console.log(foo('mon'))    //  8

Say, Saturday and Sunday we sleep in.

case 'sat':
case 'sun': return 9

Test cases:

console.log(foo(''))       //  8
console.log(foo('mon'))    //  9

And Wednesday to Friday we burn the midnight oil…

case 'wed':
case 'thu':
case 'fri': return 7

Test cases:

console.log(foo(''))       //  8
console.log(foo('mon'))    //  7

Of course we should by now have seen how the fall through is working and there is still no definition for Monday and Tuesday.

case 'mon':
case 'tue': return 8

Test cases:

console.log(foo(''))       //  8
console.log(foo('mon'))    //  8

Put it all to the test…

function foo(x) {
  switch(x) {
  case 'mon':
  case 'tue': return 8
  case 'wed':
  case 'thu':
  case 'fri': return 7
  case 'sat':
  case 'sun': return 9
  default: return 8
  }
}
console.log(foo(''))       //  8
console.log(foo('mon'))    //  8
console.log(foo('wed'))    //  7
console.log(foo('sat'))    //  9

Interesting!!! ok got it!! thanks so much for your help! That was a sneaky error lol :grinning_face_with_smiling_eyes:

1 Like

This looks awesome! i love cleaning up my code, thanks so much for this advice!

1 Like

Glad to help. And yeah … It is both silly and amazing that such small mistakes can break the whole thing :grinning_face_with_smiling_eyes:

Can someone explain what i may have missed.

let dayOfWeek = {
monday: 8,
tuesday: 7,
wednesday: 9,
thursday: 5,
friday: 10,
saturday: 12,
sunday: 11,
};

const getSleepHours = (day) => {
return dayOfWeek[day];
};

const getActualSleepHours = () => {
const actualSleepHours = 0;
for (let day in dayOfWeek) {
actualSleepHours += getSleepHours(day);
}
return actualSleepHours;
};

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

function calculateSleepDebt() {
const actualSleepHours = getActualSleepHours();
const idealSleepHours = getIdealSleepHours();
const sleepHourDifference = actualSleepHours - idealSleepHours;
if (actualSleepHours === idealSleepHours) {
console.log(“You got the perfect amount of sleep”);
} else if (actualSleepHours > idealSleepHours) {
console.log(
You got more sleep than needed. You slept ${sleepHourDifference} more than ideal
);
} else {
console.log(
You should get more rest. You slept ${sleepHourDifference} less than ideal
);
}
}

getActualSleepHours();
getIdealSleepHours();
calculateSleepDebt(“monday”);

Can you please explain what, you, think you may have missed?

i cant seem to be able to get the sum of the array using a for in loop and returning the result to the getActualSleepHours function

1 Like

Now we know that, it gives us a goal. Have you gone back to review steps?

yes i have. also tried running the program i get this error on my console.

actualSleepHours += getSleepHours(day);
^

TypeError: Assignment to constant variable.
at getActualSleepHours

That would be something to follow up.

is the for in loop written correctly?