sleep.Debt.Calculator.js

I’m getting the following error:

/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/sleepDebtCalculator.js:38
if (actualSleepHours === idealSleepHours)
^

ReferenceError: actualSleepHours is not defined
at Object. (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/sleepDebtCalculator.js:38: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

I thought I defined actualSleepHours on line 34? I’ve pasted my code below for your reference.

Thank you.

const getSleepHours = day =>{
if (day === 'monday'){
  return 8;
} else if (day === 'tuesday'){
  return 5;
} else if (day === 'wednesday'){
  return 6;
} else if (day === 'thursday'){
  return 3;
} else if (day === 'friday'){
  return 3;
} else if (day === 'saturday'){
  return 3;
} else if (day === 'sunday'){
  return 5;
} else {
  return 'error';
}
};

const getActualSleepHours = () => {
return getSleepHours('monday') + getSleepHours('tuesday') + getSleepHours('wednesday') + getSleepHours('thursday') + getSleepHours('friday') + getSleepHours('saturday') + getSleepHours('sunday')
};
console.log(getSleepHours('monday'));
console.log(getActualSleepHours());

const getIdealSleepHours = () => {
  const idealHours = 7.5;
  return idealHours * 7;
};
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 more rest.');
} 
else {
  console.log('Something went wrong. Please check your code.')
};

you have, but then two lines later your function ends:

const calculateSleepDebt = () => {
const actualSleepHours = getActualSleepHours();
const idealSleepHours = getIdealSleepHours();
}; // curly bracket closes the function

the variable has a local scope, it only exists within the body of the function

trying to access this variable outside the function will result in an error

1 Like

Ok, I think I see what I need to do.

Thank you!

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.