My code is returning 'undefined' for the getActualSleepHours calculation

const getSleepHours = day => {
switch (day) {
case ‘monday’:
return 8;
break;
case ‘tuesday’:
return 9;
break;
case ‘wednesday’:
return 7;
break;
case ‘thursday’:
return 6;
break;
case ‘friday’:
return 5;
break;
case ‘saturday’:
return 10;
break;
case ‘sunday’:
return 12;
break;
}
};

const getActualSleepHours = () => {
getSleepHours(‘Monday’) +
getSleepHours(‘Tuesday’) +
getSleepHours(‘Wednesday’) +
getSleepHours(‘Thursday’) +
getSleepHours(‘Friday’) +
getSleepHours(‘Saturday’) +
getSleepHours(‘Sunday’)
};

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

console.log(getActualSleepHours());

console.log(getIdealSleepHours());

Hello! Do you explicitly return anything from the getActualSleepHours function? If a function has no return, the value is automatically “set” (for lack of a better word) to undefined.

in the function you only add them up, but you don’t do anything with the new value, right?
If you would put the getSleepHours adding part in a return, that should work.

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

Also, please use more whitespace, the code provided is hard to read

Thanks the both of you. However, the task says to use an implicit return. Doesn’t this mean that I don’t use the return keyword?

So, I removed the curly braces in order to implicitly return the value and now it’s saying NaN instead of undefined!

I figured out what went wrong when it was returning NaN - it was the casing! All lower case in the switch statement and then the first letter was a capital in the function getACtualSleepHours.

1 Like