Sleep debt calculator error

Hey everyone !

Sorry for the question, I just finished my Sleep Debt calculator but can’t understand why it’s not working ? Everything seems fine…

Here is my code :

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 8;
}else if (day === ‘saturday’) {
return 7;
} 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;
}
console.log(getActualSleepHours());
console.log(getIdealSleepHours());

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

if (actualSleepHours === idealSleepHours) {
console.log(‘You got the perfect amount of sleep’);
} else if (actualSleepHours > idealSleepHours) {
console.log(‘You got more sleep than you needed’);
} else if (actualSleepHours < idealSleepHours) {
console.log(‘You should get some rest’);
}
if (actualSleepHours < idealSleepHours) {
console.log(‘You got ’ + (idealSleepHours - actualSleepHours) + ’ hour(s) less sleep than you needed this week. Get some rest.’);
}
calculateSleepDebt();

And here is the error screen :

49
56
/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/sleepDebtCalculator.js:41
if (actualSleepHours === idealSleepHours) {
^

ReferenceError: actualSleepHours is not defined
at Object. (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/sleepDebtCalculator.js:41:5)
at Module._compile (module.js:571:32)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)
at bootstrap_node.js:542:3

Can somebody explaining why this isn’t working please ?
Thank you so much in advance !

1 Like

You have accidentally closed the calculateSleepDebt() function before the if statements, therefore they cannot actually see the actualSleepHours variable as it’s outside of their scope. Move the if statements inside of the function and everything should work as intended!

2 Likes

Awesome ! Thank you so much !! I still have difficulties with that :confused:

Thank you again for your time !

For future reference: How Do I Format Code In My Posts?

1 Like