Help for Sleep Debt calculator

Hello everybody,

After 1 hours of re reading I just don’t find the issue, I keep having “actualSleepHours is undefined” as an error code. But I checked the typo, I checked numerous time the code on the solution, until the last function, my code works fine… I guess it’s probably a stupid mistake, but it’s driving me crazy (especially as I finally understood and felt quite comfortable for the whole exercise… Frustration 100%). Here’s my code, thank you in advance !

And I would like to understand why I have this error code ? Because I declared the variable, actualSleepHours associated to the function getActualSleepHours, and the function was working fine…

function getSleepHours(day) {
  if (day === 'Monday') {
    return 8; 
  } else if(day === 'Tuesday') {
    return 7;
  } else if (day === 'Wednesday'){
    return 5;
  } else if (day === 'Thursday') {
    return 6;
  } else if (day === 'Friday') {
    return 8;
  } else if (day === 'Saturday') {
    return 10;
  } else if (day === 'Sunday') {
    return 9;
  }
}

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

console.log(getSleepHours('Tuesday'));
console.log(getActualSleepHours());

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

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

if (actualSleepHours === idealSleepHours) {
  console.log('You\'re sleeping the perfect amount');
 } else if (actualSleepHours > idealSleepHours){
    console.log('You got more sleep than you need!');
  } else if (actualSleepHours < idealSleepHours) {
    console.log('You should sleep more');
  }
  else {
    console.log('Error! Something went wrong.');
  }

calculateSleepDebt();


















here is your function:

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

in which you define local variable. But then outside/after the function, you try to access these variables. Which will result in an error because of the scope issue (you can’t access variable outside there scope).

Hm, thank you, I think I understand what you explain but I don’t get how to solve the issue then. How do I access the variables outside of the scope ?

Sorry if I don’t explain myself well, english is not my first language.

you don’t, that is the whole idea of scope. You should think about what should be inside and outside the function, so the operations you want to do have access to the variables.

currently your function only declares two variable, that is it. Why are the comparisons not part of the function body?

Oh I get it ! I didn’t understood scope the right way indeed.
Thank you for making me think and not giving the solution right away !

1 Like

Hey script,

So, for you to have acess to the getSleepHours variables inside the function , you need to write the const getActualSleepHours inside the body { } of the first function, like stetim94 wrote in the first post :

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