What is wrong with my switch statement?


#1

It seems like I didn’t create a proper switch statement because my getActualSleepHours function shows up as undefined. What am I doing wrong?

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

case 'monday':
  return 6;
  
case 'tuesday':
  return 6.5;
  
case 'wednesday':
  return 7;
  
case 'thursday':
  return 7.5;
  
case 'friday':
  return 6; 
  
case 'saturday':
  return 8.5 ;
  
default: console.log('Not a day');
                   }

};

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

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

const calculateSleepDebt = () => {
const actualSleepHours = getActualSleepHours();
const idealSleepHours = getIdealSleepHours();
let sleepDebt = idealSleepHours - actualSleepHours;
let sleepBank = actualSleepHours - idealSleepHours;
console.log(‘actual sleep hours ’ + actualSleepHours);
console.log(‘ideal sleep hours ’ + idealSleepHours);
if (actualSleepHours === idealSleepHours){
console.log(‘You got’ + actualSleepHours + ’ hours of sleep… perfect’);
}
else if (actualSleepHours > idealSleepHours){
console.log(‘You got’ + sleepBank + ’ hours more than enough sleep’);
}

else if (actualSleepHours < idealSleepHours){
console.log(‘You need’ + sleepDebt + ’ hours more sleep’);
}
else {console.log(‘error’)}
};

calculateSleepDebt();


#2

Hi @coursesurfer43988,

Your switch statement is all good.


Just that this part of your code:

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

You should return the sum of all getSleepHours(‘sunday’) + … , see below:

const getActualSleepHours = () => { 
    return getSleepHours('sunday') + getSleepHours('tuesday') + getSleepHours('monday') + getSleepHours('wednesday') + getSleepHours('thursday') + getSleepHours('friday') + getSleepHours('saturday') ;
};

Because when you return empty code, it throws your getActualSleepHours() function into showing undefined. Changing just that, your code will work wonderfully.

Also,

console.log('You got ' + actualSleepHours + ' hours of sleep… perfect');
                    ^
console.log('You got ' + sleepBank + ' hours more than enough sleep');
                    ^
console.log('You need ' + sleepDebt + ' hours more sleep');
                     ^

You need an extra space at the pointer to make your sentences print nicely. :wink:


Side note: In case you have more questions in future, it’s good to have a look on how to format code here. Just use the </> symbol to format the codes instead of the ".

Cheers :slight_smile:


#3

Thanks, I certainly would have never realized that without your help!