Sleep debt calculator. variable not defined output problem

why i get an error saying idealSleepHours not defined when i did indeed defined it?
const calculateSleepDebt = () => {
const actualSleepHours = getActualSleepHours();
const idealSleepHours = getIdealSleepHours() ;
const getSleepHours = day => {
switch(day){
case ‘Monday’:
return 8;
case ‘Tuesday’:
return 7;
case ‘Wednesday’:
return 8;
case ‘Thursday’:
return 9;
case ‘Friday’:
return 6;
case ‘Saturday’:
return 10;
case ‘Sunday’:
return 8;
}
};

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

const getIdealSleepHours = () =>{
let idealHours = 8;
return idealHours*7;
}
const calculateSleepDebt = () => {
const actualSleepHours = getActualSleepHours();
const idealSleepHours = getIdealSleepHours() ;
}
if (actualSleepHours = idealSleepHours ) {
console.log(You got the perfect amount of sleep this week) ;
}

hoisting (definitions being available all over the scope) is available for function declarations – not for variables defined with const keyword. Your variables defined with const are available below the lines where it was defined – not above it.

1 Like