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.
Thanks for reaching out This took me some time to figure out 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. (())
Hi, thank you so much for your reply, i did find that i missed “()” in the name part of the function:
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 but if you could please elaborate, i would very much appreciate that. and thanks for your reply so far
I am new myself, that’s why my explanation wasn’t so good 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
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();
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
);
}
}