Sleep-Debt-Calculator Error

Hello Everyone, I have an issue with my code, if possible, can anyone find the issue for me please?

const getSleepHours = day => {

switch(day) {
case ‘monday’:
return 8;
break;
case ‘tuesday’:
return 7;
break;
case ‘wednesday’:
return 8;
break;
case ‘thursday’:
return 5;
break;
case ‘friday’:
return 8;
break;
case ‘saturday’:
return 7;
break;
case ‘sunday’:
return 8;
break;
default:
return “Error!”;
}
}

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

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

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

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

if (actualSleepHours === idealSleepHours) {
console.log(“You’ve got the perfect amount of sleep!”);
}

else if(actualSleepHours > idealSleepHours) {
console.log(“You’ve got more sleep than needed”);
}

else if(actualSleepHours < idealSleepHours) {
console.log(“You should get some more rest”);
}

else {
console.log(“Error! Something went wrong, please check your code”);
}
};

calculateSleepDebt();

If you copy/paste your code directly into the posts, the formatting is lost and it is difficult to read the code and identify any mistakes. To preserve code formatting in forum posts, see: [How to] Format code in posts

You have three issues in the getActualSleepHours function and a typo within the calculateSleepDebt function.

  • Issue #1: In the getActualSleepHours function, you have the + operator at the end of a statement. It should be removed. The + operator at the end of the other lines indicates that the statement isn’t finished but is spread over multiple lines. So, its use is valid for those lines. But, the very last line shouldn’t have the + operator since we have reached the end of the multi-line statement.

  • Issue #2: The closing curly brace for the getActualSleepHours function is missing.

  • Issue #3: Since you are using curly braces for the body of the getActualSleepHours arrow function, so you need to use the return keyword for an explicit return. If you want to omit the return keyword and do an implicit return, you need to remove the curly braces, If you use curly braces, you can’t return implicitly. If you omit curly braces, you can’t return explicitly.

With the above in mind:

const getActualSleepHours = () => {
    return getSleepHours('monday') +  // Issue #3 solved by adding the return keyword
    getSleepHours('tuesday') +
    getSleepHours('wednesday') +
    getSleepHours('thursday') +
    getSleepHours('friday') +
    getSleepHours('saturday') +
    getSleepHours('sunday')  // Issue #1 solved by removing + from end of statement 
}  // Issue #2 solved by adding closing curly brace for body of function
  • Issue #4: In the calculateSleepDebt function, you have a typo:
// You wrote:
const actualSleepHours = getAcutualSleepHours();

// It should be:
const actualSleepHours = getActualSleepHours();
2 Likes

Thank You so much, that really helped me out a lot