My code keeps outputting you got NaN hours of sleep instead of the number

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
const getSleepHours = (day) => {
if (day === monday) {
return 8;
} else if (day === tuesday) {
return 7;
} else if (day === wednesday) {
return 4;
} else if (day === thursday) {
return 8;
} else if (day === friday) {
return 10;
} else if (day === saturday) {
return 10;
} else if (day === sunday) {
return 7;
}
};

const getActualSleepHours = () =>
getSleepHours(“monday”) +
getSleepHours(“tuesday”) +
getSleepHours(“wednesday”) +
getSleepHours(“thursday”) +
getSleepHours(“friday”) +
getSleepHours(“saturday”) +
getSleepHours(“sunday”);

const getIdealSleepHours = () => {
const getIdealHours = 8;
return getIdealHours * 7;
};

console.log(getActualSleepHours()); // should print the sum of all sleep hours in the week

console.log(getIdealSleepHours()); // if idealHours is 8, should print 56

if (getActualSleepHours === getIdealSleepHours) {
console.log(perfect amount of sleep);
} else if (getActualSleepHours > getIdealSleepHours) {
console.log(you have overslept);
} else if (getActualSleepHours < getIdealSleepHours) {
console.log(you need some rest);
}
if (getActualSleepHours < getIdealSleepHours) {
console.log(
“You got " +
(getIdealSleepHours - getActualSleepHours) +
" hours less sleep than you needed this week. Get some rest.”
);
}

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:

Recall that monday is a string so must be in quotes. Same applies down the line.

1 Like

You have the functions there, but you did not call them, so this is checking whether the functions are equal, not whether the results of calling the functions are equal.

getActualSleepHours should be getActualSleepHours()
getIdealSleepHours should be getIdealSleepHours()

alternative using variables

A better approach is to store the result of the function calls as variables (numbers, in this case):

const actualSleepHours = getActualSleepHours();
const idealSleepHours = getIdealSleepHours();

if (actualSleepHours === idealSleepHours) {
  console.log(`perfect amount of sleep`);
} else if (actualSleepHours > idealSleepHours) {
  console.log(`you have overslept`);
} else if (actualSleepHours < idealSleepHours) {
  console.log(`you need some rest`);
}
if (actualSleepHours < idealSleepHours) {
  console.log(
      "You got " +
      (idealSleepHours - actualSleepHours) +
      " hours less sleep than you needed this week. Get some rest."
  );
}

Also, for the posts in the forums, please use the </> button to post your code so that the post keeps the formatting you used in your code. After clicking on the </>, paste your code between the ``` and ```
(and the these ``` should be on their own line)