Sleep Debt Calc

Hi, again the else if statements are giving me a hard time.

Are anybody able to see what the ■■■■ I am doing wrong? :slight_smile:

const getSleepHours = day => {
switch(day) {
case ‘monday’:
return 8;
case ‘tuesday’:
return 8;
case ‘wednesday’:
return 8;
case ‘thursday’:
return 8;
case ‘friday’:
return 8;
case ‘saturday’:
return 8;
case ‘sunday’:
return 8;
}
};

const getActuallySleepHours = () =>
getSleepHours(‘monday’) +
getSleepHours(‘tuesday’) +
getSleepHours(‘wednesday’) +
getSleepHours(‘thursday’) +
getSleepHours(‘friday’) +
getSleepHours(‘saturday’) +
getSleepHours(‘sunday’);

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

function calculateSleepDebt (){
const actualSleepHours = getActualSleepHours();
const idealSleepHours = getIdealSleepHours();
}
if (actualSleepHours === idealSleepHours) {
console.log(‘Great, you have got the perfect amount of sleep!’);
} else if (actualSleepHours > idealSleepHours){
console.log(‘Wauv, you got more sleep than you need.’);
} else if (actualSleepHours < idealSleepHours){
console.log('Ouch, you ’ + (idealSleepHours - actualSleepHours) + ‘hour(s) less sleep than you needed this week. Get some rest.’);
};

calculateSleepDebt();

1 Like

You’re else if statemenets are fine. The problem is theyre location tho. In you’re if else statement you are using the variable actualSleepHours this variable is scoped tho since its being created in the function calculateSleepDebt () and is therfore only available for checks inside that function.
You can fix this by placing the if block inside the function as shown below.

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

	if (actualSleepHours === idealSleepHours) {
		console.log('Great, you have got the perfect amount of sleep!');
	} else if (actualSleepHours > idealSleepHours){
		console.log('Wauv, you got more sleep than you need.');
	} else if (actualSleepHours < idealSleepHours){
		console.log('Ouch, you ' + (idealSleepHours - actualSleepHours) + 'hour(s) less sleep than you needed this week. Get some rest.');
	}
}

lastly you have a typo in you’re code that throws an error. getActuallySleepHours should be getActualSleepHours.

1 Like

I’m stuck on the sleep debt calculator, too. I’m not finished - I didn’t get that far. It keeps dying at line 4, with “SyntaxError: Unexpected token {”

I can’t find an extra curly bracket, though.

Help, please?

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


function 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');




1 Like

Hey @technomom,

It drops out and gives an error because you are missing some syntax. And the error messages says where it goes wrong ;). Hope this helps.

If you really, really cant find it, you can use the spoiler below to see where the errors are.

if (day === 'monday') {
    return 8;
  } else if (day === 'tuesday') { // you were missing a { here
    return 8;
  } else if (day === "wednesday") {  // you were missing a } at the start of this line
    return 8;
  }
2 Likes

Thank you!
I put those in and corrected a couple of other things, and still get the same error for the same line. When I do a find, it says I have too many { or not enough }, but I can’t find where they aren’t mated up.

Thanks in advance!

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

function 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');

1 Like

Hey,

Your conditions should be before the opening brackets like you did at if.

....
} else if (day === "wednesday") {
    return 8;
} ....
2 Likes

Also I see you added a lot of surplus brackets… Code will definitely break on that. And I see you have two functions with the same name getSleepHours.

2 Likes

Thank you very much @janneslohmeijer !

2 Likes

I’m sorry, I’m not usually this dense. I don’t see any checkboxes.

No worries, I realized this is not your threat but that of someone else so you can’t see and check the solved box ;).

Hi, I got stuck with this one. When I run the code, getActualSleepHours, it doesn’t give me 36, but 30error3. :S I have no clue why…

const getSleepHours = (day) => {
if (day === ‘Monday’) {
return 8;
} else if (day === ‘Tuesday’) {
return 7;
}
else if (day === ‘Wednesday’) {
return 6;
}
else if (day === ‘Thursday’) {
return 5;
}
else if (day === ‘Friday’) {
return 4;
}
else if (day === ‘Sturday’) {
return 3;
}
else if (day === ‘Sunday’) {
return 3;
} else {
return ‘error’;
}
}

const getActualSleepHours = () => {
return getSleepHours(‘Monday’) + getSleepHours(‘Tuesday’) + getSleepHours(‘Wednesday’) + getSleepHours(‘Thursday’) + getSleepHours(‘Friday’) + getSleepHours(‘Saturday’) + getSleepHours(‘Sunday’);
}

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

console.log(getSleepHours(‘Tuesday’));
console.log(getActualSleepHours());

You got a typo :wink: See below: