Sleep debt calculator

Hi everyone. Running this code only returns ’ error , please check code for error. Here is my code. please can someone help identify the bug here. Thanks

const getSleepHours = day => {

switch(day) {

case ‘monday’:

return 8;

break;

case ‘tuesday’:

return 5;

break;

case ‘wednesday’:

return 7;

break;

case ‘thursday’:

return 8;

break;

case ‘friday’:

return 8;

break;

case ‘saturday’:

return 6;

break;

case ‘sunday’:

return 6;

break;

default:

return ‘error, try to sleep genius’

}

};

const getActualSleepHours = () => {

getSleepHours (‘monday’) + getSleepHours (‘tuesday’) + getSleepHours (‘wednesday’) + getSleepHours (‘thursday’) + getSleepHours (‘friday’) + getSleepHours (‘saturday’) + getSleepHours (‘sunday’);

};

const getIdealSleepHours = () =>{

const idealHours = 8;

return idealHours * 7;

};

const calculateSleepDebt = () => {

const actualSleepHours = getActualSleepHours();

const idealSleepHours = getIdealSleepHours();

if (actualSleepHours === idealSleepHours){

console.log(“perfect amount of sleep”)

} else if (actualSleepHours > idealSleepHours){

console.log(“you are sleeping more”)

} else if (actualSleepHours < idealSleepHours){

console.log(“you are slpeeping less hours”)

} else{

console.log(“Error, pls put in right code”);

}

};

calculateSleepDebt ();

Hi!

Can you please share a screenshot of the error message you are receiving?

Also in future please format any shared code according to this guide, it makes it much easier to see where the error is :smile:

Ah actually I think I see it:

const getActualSleepHours = () => {

getSleepHours ('monday') + getSleepHours ('tuesday') + getSleepHours ('wednesday') + getSleepHours ('thursday') + getSleepHours ('friday') + getSleepHours ('saturday') + getSleepHours ('sunday');
};

This function returns nothing, so when you attempt to call it and save it into actualSleepHours with const actualSleepHours = getActualSleepHours(); , the value is equal to undefined, hence why it cannot be properly used in the comparison. You will have to return the result of adding the getSleepHours calls.

2 Likes

Hi, Thank you. The problem was actually with that line of code and also because the block is implicitly returned but i did forget that and added curly braces.

1 Like

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