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

return ‘Input is not a valid day’;
break;
}
you forgot the Curly brackets
it should look like this:
return ‘Input is not a valid day’;
break;
}
};

But how come in the Get Help code along video
(https://www.youtube.com/watch?v=48ko-s_ePbc&feature=emb_title)
She calls the function at the end using console.log(getActualSleep()); with no parameters and it returns all of the values added, when i try doing it i get error. I am literally doing same exact things she is doing.

This is my code. Which 100% matches what she is doing in the video. Please explain!!!

const getSleepHours = day => {
  
    switch(day) {
      case 'monday':
        return 7;
      case 'tuesday':
        return 7;
      case 'wednesday':
        return 7;
      case 'thursday':
        return 7;
      case 'friday':
        return 5;
      case 'saturday':
        return 3;
      case 'sunday':
        return 7;
      default: 
        return 'Error!';
    }    
};

const getActualSleepHours = () => 
  
  getSleepHours('monday') +
  getSleepHours('tuesday') +
  getSleepHours('wednesday') +
  getSleepHours('thursday') +
  getSleepHours('friday') +
  getSleepHours('saturnday') +
  getSleepHours('sunday');
  
console.log(getSleepHours('monday'));  
console.log(getActualSleepHours());

Output: 
7 
33Error!7

Well, clearly one of the cases gets to the default, so a logic step would to inspect which value this is:

      default: 
        console.log(day);
        return 'Error!';

turns out you made a typo, so not a 100% match :wink:

I though default worked like else in a if/else statement?

but how come she is not doing the code below in the video?

default: 
        console.log(day);
        return 'Error!';

im just concerned why it works for her and not for me.
Please watch the video, maybe im missing something.

it does, so none of the cases/if/else if statement are a match. The most obvious step in then solving the problem is figuring out which value this is.

nope, not going to. I already found your mistake. Did you try to run your code while using the console.log i provided? This should help you narrow down the problem.

3 Likes

I just ran the code you provide and still get the same output:

7
saturnday
33Error!7

const getSleepHours = day => {
  
    switch(day) {
      case 'monday':
        return 7;
      case 'tuesday':
        return 7;
      case 'wednesday':
        return 7;
      case 'thursday':
        return 7;
      case 'friday':
        return 5;
      case 'saturday':
        return 3;
      case 'sunday':
        return 7;
      default: 
        console.log(day);
        return 'Error!';
    }    
};

const getActualSleepHours = () => 
  
  getSleepHours('monday') +
  getSleepHours('tuesday') +
  getSleepHours('wednesday') +
  getSleepHours('thursday') +
  getSleepHours('friday') +
  getSleepHours('saturnday') +
  getSleepHours('sunday');
  
console.log(getSleepHours('monday'));  
console.log(getActualSleepHours());

That’s different output.

When things go wrong you’ll typically want to observe what happened. Writing things out with console.log is a straight-forward way to do that.

Your default case wasn’t supposed to match, so you’d want to know which value didn’t match and printing it out shows you that, that’s what @stetim94 added, it wasn’t supposed to change the result.

1 Like

What can i do different so when i call console.log(getActualSleepHours()); with no parameters it gives me all of the hours of sleep added as an output?

The problem isn’t in getActualSleepHours or its lack of parameters

The problem is that you have a switch which has 7 valid cases and one invalid that should never happen because the 7 matching ones should always match.

What would cause a case to not match? Or conversely, why would a case match?

So you’re sending in some value that doesn’t match. Either there would be something wrong with what is considered valid, or an invalid value is going in.

switch (3) {
  case 5:
    console.log('the value was 5')
    break
  default:
    console.log("oops, something didn't get matched.")
}

So when you wrote this you should have been thinking, okay here’s this default case where I can spot that something bad happens, it should never happen since all my seven days are already covered.

And then, oh look my default case matched, did I get one of the cases wrong or did I send in something silly?

Followed by observing what happened (inserting console.log to follow along and get information)

1 Like