Introduction To JavaScript Sleep Debt Calculator

https://www.codecademy.com/courses/introduction-to-javascript/projects/sleep-debt-calculator

Can’t figure out why I keep getting a error message on the backend from the if statements. It is defaulting to less thand and throwing these results. As you load the code you will see that there are plenty of console.log statements to check the code throughout.

Input is not a valid day
56
42
56
35
User got more sleep than needed
undefined

const getSleepHours = day => {
  if (day === 'monday') {
    return 8;
  } else if (day === 'tuesday') {
    return 8;
  } else if (day === 'wednesday') {
    return 8; 
  } else if (day === 'thursday') {
    return 8;
  } else if (day === 'friday') {
    return 8;
  } else if (day === 'saturday') {
    return 8;
  } else if (day === 'sunday') { 
  	return 8;
  } else {
    return 'Input is not a valid day'
  }
}
//Test function with console log printout.
console.log(getSleepHours('tesday'))

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

//Test function output
console.log(getActualSleepHours())

const getIdealSleepHours = (num) => {
  const idealHours = num
  return idealHours * 7;
}
  
console.log(getIdealSleepHours(6))

const calculateSleepDebt = () => {
  actualSleepHours = getActualSleepHours()
  console.log(actualSleepHours)
  idealSleepHours = getIdealSleepHours(5)
  console.log(idealSleepHours)
  if (actualSleepHours === idealSleepHours) {
    console.log('User got the perfect amount of sleep')} 
  else if (actualSleepHours > idealSleepHours) {
    console.log('User got more sleep than needed')}
  else if (actualSleepHours > idealSleepHours ){
    console.log('User got less sleep than needed')}
  else {
    console.log('There is an error!')}
}
console.log(calculateSleepDebt())

Check out my comments in your code. Hope this helps!

2 Likes
const getSleepHours = day => {
	switch (day) {
    case 'monday':  
    	return 8;
      break;
   case 'tuesday':  
    	return 7;
      break;
   case 'wednesday':  
    	return 5;
      break;  
  	case 'thursday':  
    	return 7;
      break;
   case 'friday':  
    	return 8;
      break;  
    case 'saturday':  
    	return 9;
      break;     
  	case 'sunday':  
    	return 9;
      break;  
    default:  
      return 'Input is not a valid day';
      break;
}
console.log(getSleepHours('tuesday'));
  

Hmm, get first error when I try to log in function!
*Can’t complete 3. point -> Test the function by calling it multiple times and printing the results to the console.

Welcome @martins.klucis,
As far as i can tell you are not closing your switch statement. I added some comments in your code that should fix it.

Hope it helps.

1 Like

Thx. for help.
When I get stuck - try to use hint feature and sometimes GET HELP option where is a video guide.
Codecademy video about sleep debt calc.
In video she use a break statement in a switch function.

Oowh yea i see. Its really not necessary tho.

Using break after return does nothing cause break will never be reached. A reasonable editor would give you a warning on this so better not use it. Codecadamy does not have this so i think the person in the video didn’t think this trough and just did this out of automaticity.

If the link is given in the exercise you could file a bug report that the video contains false information. Maybe they will change the video in the future. Don’t count on it tho since it does take very long to create a single video.

1 Like