Sleep Debt Calculator printing multiple results to the console?

Hi there. Working on the SDC project and while it seems to be running correctly, I can’t figure out why it’s returning multiple possible outcomes when I’ve specified ===, <, or >. I thought I learned from the Race Day exercise that neither < nor > should be inclusive of an equal comparison. I’m also not sure why the program doesn’t stop upon finding one of these statements to be true.

Here’s my code:

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;
  }
};

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

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

const calculateSleepDebt = () => {
 const actualSleepHours = getActualSleepHours();
 const idealSleepHours = getIdealSleepHours(); 

 if (actualSleepHours === idealSleepHours) {
   console.log('Perfect sleep.');

 } else if (actualSleepHours > idealSleepHours) {
   console.log('Too much sleep. You got ' + (actualSleepHours - idealSleepHours) + ' hours more sleep than you needed.');

 } else if (actualSleepHours < idealSleepHours); {
   console.log('Get some sleep. You got ' + (idealSleepHours - actualSleepHours) + ' hours less sleep than you need this week.');
 }
 };

 calculateSleepDebt();

When executed the console returns:

Perfect sleep.
Get some sleep. You got 0 hours less sleep than you need this week.

When it clearly skipped the second possible result, why did it bother with the last one once it found the first result to be true? Thanks much.

Hi welcome to the forums!

  1. Be very careful to check for syntax, usually the area where you think the code is running funny is prone to having a tiny syntax slip that throws things off

  2. If the syntax is clean, I find for issues like these it’s really useful to insert “tracer code” inside key points of the code to figure out what is happening under the hood.

For extra clarity, I like formatting it in a way that tells me what I’m looking at specifically:

console.log(`Current value for actualSleepHours: ${actualSleepHours}`) 
//will print 
//Current value for actualSleepHours: (and whatever value it's equal to at that point)

(note the use of backticks ` in the console.log string)

At least then you’ll be able to know if it’s an issue of logic.

  1. You’re right about the concept of if/else, once it hits the conditions for the first if it technically shouldn’t keep going: So then, would the reason be syntax or logic?

Thank you. It’s always easy enough to suspect a syntax error, but this early on in practice, when much of my understanding of syntax is still guess work, it’s largely impossible to be my own editor. Are there other tools I can use to check my syntax when errors like these are not effecting the outcome to such a degree that the console returns an error?

1 Like

There are!

When I first got into programming (Java) I used to hate error messages… they seemed cryptic and just a roadblock. Now, strangely, I feel like they are the springboard to help my code run well, and 8 times out of 10 will usually be the key to fixing my code.

VS Code and Sublime are two popular editors (sublime less so for js, but it can be easily configured… I use it a lot with Python so I just stuck with it). They can catch a lot of the smaller syntax things for sure.

Some mindset adjustments can also help when writing code (so that even without those editors these problems happen less). Trust your code less. For every line you’re unsure about, make it prove to you that it’s working by using a console.log() or calling a function to see if it throws an error. It seems like overkill at first, but within the first week of adapting this you’ll find you don’t need to do at all and can catch your mistakes on the fly. And with practice you can spot potential syntax errors in others’ code quickly as well.

The third way is to have a very clear checkbox of syntax rules in your head. I personally have an aversion for the loose way Javascript works with syntax (as opposed to python, for example) but I deal with it by taking a little extra time to format it in a consistent way. That way, when mistakes happen, it’s more likely a typo than a trend.

And then finally, tracer code and commenting sections of your code in and out depending on context is a good tool. Sometimes blunt, but in the beginning it can be useful.

Good luck!