Sleep debt calculator

Been stuck for a little while on a couple pieces of this project. 1) I keep getting the need more rest result no matter what values are input 2) I am getting NaN for how much sleep the user is ‘missing’. Any help is greatly appreciated as I cannot seem to spot my error.

Here’s my code:

const getSleepHours = day => {
 switch(day) {
  case 'Monday':
  return 8
  break;
  case 'Tuesday':
  return 7
  break;
  case 'Wednesday':
  return 7.5
  break;
  case 'Thursday':
  return 8
  break;
  case 'Friday':
  return 7
  break;
  case 'Saturday':
  return 7.5
  break;
  case 'Sunday':
  return 6
  break;
  default:
  return 'ERROR';
  
 }
}
const getActualSleepHours = () => 
  'You slept: ' +
  (getSleepHours('Monday') + 
  getSleepHours('Tuesday') +
  getSleepHours('Wednesday') +
  getSleepHours('Thursday') +
  getSleepHours('Friday') +
  getSleepHours('Saturday') +
  getSleepHours('Sunday'));

const getIdealSleepHours = () => {
  let idealHours = 8;
  return 'Your sleep goal is: ' + idealHours * 7
}

const calculateSleepDebt = () => {
  const actualSleepHours = getActualSleepHours();
  const idealSleepHours = getIdealSleepHours();
  if(actualSleepHours === idealSleepHours){return 'You met your sleep Goal! Well done!';
  }
  else if(actualSleepHours > idealSleepHours) {
    return 'Okay sleepyhead!! You got ' + (actualSleepHours - idealSleepHours) + 'extra sleep this week!';
  }
  else if(actualSleepHours < idealSleepHours) {
    return 'You N E E D more rest! Like about ' + (idealSleepHours - actualSleepHours) + ' hours less than you should have';
  }
  else {
    return 'ERROR!!';
  }
};

console.log(getIdealSleepHours());
console.log(getActualSleepHours());
console.log(calculateSleepDebt());

In your getActualSleepHours and getIdealSleepHours functions, you are returning strings instead of numbers. Consequently, in calculateSleepDebt, stings are assigned to the actualSleepHours and idealSleepHours variables. You can confirm this by adding a couple of statements for debugging purposes,

const calculateSleepDebt = () => {
  const actualSleepHours = getActualSleepHours();
  const idealSleepHours = getIdealSleepHours();
  
  console.log(actualSleepHours); // "You slept: 51"
  console.log(idealSleepHours); // "Your sleep goal is: 56"
  
  if(actualSleepHours === idealSleepHours){ 
...

The string "You slept: 51" comes before the string "Your sleep goal is: 56" in lexicographical order. Hence the condition if (actualSleepHours < idealSleepHours) evaluates to true. Since the strings are not numbers, so trying to subract the two results in NaN or Not a Number.

You can fix this by having the functions return numbers instead of strings,

const getIdealSleepHours = () => {
  let idealHours = 8;
  // return 'Your sleep goal is: ' + idealHours * 7
  return idealHours * 7
}

const getActualSleepHours = () => 
  // 'You slept: ' +
  (getSleepHours('Monday') + 
  getSleepHours('Tuesday') + ...

Have the functions return numbers. If you want to print strings for better readability, you have lots of flexibility in how to manage it. For Example: Within the functions, you can assign the calculated values to a variable, print your strings and then finally return the calculated value as a number.

1 Like

Thank you! This made sense right away, it seems like I was thrown off by making things a little more complicated than they needed to be.

1 Like