Help understand functions in 'Sleep Dept Calculator' project

Hi, was able to finish the project but I need help understanding why the following code console.logs the ‘else’.
The project instructs to create new variables and set them equal to the getIdealSleepHours() and ‘getActualSleepHours()’. Why is this needed in order for the functions be part of the new "calculateSleepDept()’ function.
Thank you!

const calculateSleepDept = () => {

  if (getActualSleepHours() === getIdealSleepHours()) {
    console.log('You had the perfect amount of sleep!');
  } else if (getActualSleepHours() > getIdealSleepHours()) {
    console.log('You slept ' + (getActualSleepHours() - getIdealSleepHours()) + ' more than needed');
  } else if (getActualSleepHours() < getIdealSleepHours()) {
    console.log(`You should get ${getActualSleepHours() - getIdealSleepHours()} more hours of sleep`);
  } else {
    console.log('Error, you enter un invalid entry!')
  }
};

calculateSleepDept();

You must select a tag to post in this category. Please find the tag relating to the section of the course you are on E.g. loops, learn-compatibility

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

Hi @beta4451224394

Can you post the rest of your code? It is possible you have an issue in one of your other functions that can’t bee seen in the code you posted.

The reason to store the numbers as variables instead of calling the same function multiple (with the same result) times is efficiency.
I think it takes less processing to access a variable than to do a function call.

1 Like

as I said before the hole project worked fine if directions were followed as instructed. I just wanted to understand better why the variables:
const actualSleepHours = getActualSleepHours();
const idealSleepHours = getIdealSleepHours();
were used to store the functions in ‘calculateSleepDept()’, instead of using the functions directly.
Here is the hole project in case you want to see it.
Thanks

const userSleepHours = day => {
switch (day) {
case ‘monday’:
return 6
break;
case ‘tuesday’:
return 7
break;
case ‘wednesday’:
return 8;
break;
case ‘thursday’:
return 6
break;
case ‘friday’:
return 8
break;
case ‘saturday’:
return 7
break;
case ‘sunday’:
return 7
break;
default:
return ‘Error’;
}
};

// console.log(userSleepHours(‘tuesday’))
// console.log(userSleepHours(‘wednesday’))
// console.log(userSleepHours(‘friday’))
// console.log(userSleepHours(‘sunday’))

const getActualSleepHours = () =>
6 + 7 + 8 + 6 + 8 + 7 + 7;

// console.log(getActualSleepHours());

const getIdealSleepHours = idealHours => idealHours * 7.5;

// console.log(getIdealSleepHours());

const calculateSleepDept = () => {
const actualSleepHours = getActualSleepHours();
const idealSleepHours = getIdealSleepHours(7.5);
if (actualSleepHours === idealSleepHours) {
console.log(‘You had the perfect amount of sleep!’);
} else if (actualSleepHours > idealSleepHours) {
console.log(You had ${actualSleepHours - idealSleepHours} more hours of sleep than needed!)
} else if (actualSleepHours < idealSleepHours) {
console.log(You had ${idealSleepHours - actualSleepHours} less hours of sleep than ideal)
} else {console.log(‘Wrong entry’)
}
};

calculateSleepDept();

as I said before the hole project worked fine if directions were followed as instructed. I just wanted to understand better why the variables:
const actualSleepHours = getActualSleepHours();
const idealSleepHours = getIdealSleepHours();
were used to store the functions in ‘calculateSleepDept()’, instead of using the functions directly.
Here is the hole project in case you want to see it.
Thanks

const userSleepHours = day => {
switch (day) {
case ‘monday’:
return 6
break;
case ‘tuesday’:
return 7
break;
case ‘wednesday’:
return 8;
break;
case ‘thursday’:
return 6
break;
case ‘friday’:
return 8
break;
case ‘saturday’:
return 7
break;
case ‘sunday’:
return 7
break;
default:
return ‘Error’;
}
};

// console.log(userSleepHours(‘tuesday’))
// console.log(userSleepHours(‘wednesday’))
// console.log(userSleepHours(‘friday’))
// console.log(userSleepHours(‘sunday’))

const getActualSleepHours = () =>
6 + 7 + 8 + 6 + 8 + 7 + 7;

// console.log(getActualSleepHours());

const getIdealSleepHours = idealHours => idealHours * 7.5;

// console.log(getIdealSleepHours());

const calculateSleepDept = () => {
const actualSleepHours = getActualSleepHours();
const idealSleepHours = getIdealSleepHours(7.5);
if (actualSleepHours === idealSleepHours) {
console.log(‘You had the perfect amount of sleep!’);
} else if (actualSleepHours > idealSleepHours) {
console.log(You had ${actualSleepHours - idealSleepHours} more hours of sleep than needed!)
} else if (actualSleepHours < idealSleepHours) {
console.log(You had ${idealSleepHours - actualSleepHours} less hours of sleep than ideal)
} else {console.log(‘Wrong entry’)
}
};

calculateSleepDept();

I guess I misunderstood. Your original post made it sound like your code was only logging the result from the ‘else’ segment of code. If you are asking when would that segment fire, it would be in the case where the prior conditional statements were unable to evaluate. For example, if either of your functions returned an ‘undefined’ value then you would get the else statement to fire.