Undefiend error after trying to sum eleemnts

Hello guys,

I am doing the project Sleep Debt Calculator.

At one point, when i try to sum the sleephours i get a logged an undefiend,

const getSleepHours = day => {
switch(day){
case ‘monday’:
return 8;
break;

case ‘tuesday’:
return 7;
break;

case ‘wensday’:
return 6;
break;

case ‘thusday’:
return 8;
break;

case ‘Friday’:
return 8;
break;}
};

const getActualSleepHours = () => {
getSleepHours(‘monday’)+getSleepHours(‘tuesday’)+getSleepHours(‘wensday’)+getSleepHours(‘thursday’)+getSleepHours(‘friday’)+getSleepHours(‘saturday’)+getSleepHours(‘sunday’);
}

console.log(getActualSleepHours())

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

console.log(getActualSleepHours());

console.log(getIdealSleepHours());

undefined
undefined
56

and this is what i get.

but i want to sum the ActualSleepHours.

Here are a few issues to consider:

  • You have misspelled wednesday as wensday both in the switch case 'wensday': as well as in the function call getSleepHours('wensday')

  • You have a spelling mistake in the switch case ‘thusday’: It should be case 'thursday': Also case 'Friday': should be case 'friday':

  • Your switch doesn’t have cases for saturday and sunday. If you don’t want to write cases for these 2 days, then you should have a default in your switch statement to handle the situation where no case matches the day.

  • You have used curly braces in the code:

const getActualSleepHours = () => {
...
}

If you use curly braces, you must use the return keyword to do an explicit return. If curly braces are omitted, then the return keyword should not be used as an implicit return will be made. See this post for further explanation regarding implicit/explicit return in arrow functions.

1 Like

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