Something wrong with my code; Code works but the surplus hours im getting is like 82 hours. and I'm getting an Undefined at the end

const getSleepHours = day => {
switch (day) {
case ‘monday’:
return 7;
case ‘tuesday’:
return 6;
case ‘wednesday’:
return 7;
case ‘thursday’:
return 14;
case ‘friday’:
return 10;

}
};

//concise body function doesnt require brackets or return//

const getActualSleepHours = () =>
getSleepHours(‘monday’)

  • getSleepHours(‘tuesday’)
  • getSleepHours(‘wednesday’)
  • getSleepHours(‘thursday’)
  • getSleepHours(‘friday’)

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

const calculateSleepDebt = () => {
const actualSleepHours = getActualSleepHours()
const idealSleepHours = getIdealSleepHours()
if (actualSleepHours === idealSleepHours) {
console.log(‘You got ’ + (idealSleepHours === actualSleepHours) + ’ hour(s). Thats perfect!’);
}

else if (actualSleepHours > idealSleepHours) {
console.log(‘You got ’ + (idealSleepHours + actualSleepHours) + ’ hour(s) more sleep than you needed this week. Good job.’);
}

else if (actualSleepHours < idealSleepHours) {
console.log(‘You got ’ + (idealSleepHours - actualSleepHours) + ’ hour(s) less sleep than you needed this week. Get some rest.’);
}
};

console.log(calculateSleepDebt());

I do not see anything wrong with your code. And when you have a function with no return statement it will always return undefined.

you used a + instead of a - here:

Also, in:

its going to say true or false instead of the number.

As for the calculateSleepDebt function, do you want the function to return a value or just log it to the screen/console ?
If the point is just to log stuff,
you can do
calculateSleepDebt();
instead of
console.log(calculateSleepDebt());

Please use the </> button to format the code to make it easier to read your code, it’ll keep it in formatted form.
(Paste the code between the ``` and ```.)

i changed the plus to minus. I want the function to tell me how much actual sleep i got over the ideal amount of sleep.

the console is returning this :

You got -4 hour(s) more sleep than you needed this week. Good job.
undefined

In that case, the subtraction is in the wrong order:
it should be
actualSleepHours - idealSleepHours

Also, you already have console.log inside the function so you don’t need that on the last line that says:
console.log(calculateSleepDebt());
You could do just
calculateSleepDebt();
instead.

(Or, you could have return idealSleepHours - actualSleepHours; at the end of the calculateSleepDebt function.)

1 Like

You were right , it had to be reversed to actualSleepHours - idealSleepHours. I don’t understand why though when my other if statement has it the other way…

Also, I took the console.log at the end out for calculateSleepDebt(); and thats what was making the ‘undefined’ appear to the console.

Its all good now. I appreciate your quick responses, thank youu!