Minor glitch in my Sleep Debt Calculator program

Hi. I recently completed the Sleep Debt Calculator, and it worked. The only thing is that I, with this code, got a message returned of “undefined” below the message telling me that I got the perfect amount of sleep. Where did this “undefined” come from? Thanks.

const getSleepHours = day => {
  if(day === 'monday') {
    return 6;
  } else if(day === 'tuesday') {
    return 6;
  } else if(day === 'wednesday') {
    return 7;
  }else if(day === 'thursday') {
    return 7;
  }else if(day === 'friday') {
    return 7;
  }else if(day === 'saturday') {
    return 8;
  }else if(day === 'sunday') {
    return 8;
  } else {
    return 'Error';
  }
};

const getActualSleepHours = () => 
  getSleepHours('monday') + getSleepHours('tuesday') + getSleepHours('wednesday') + getSleepHours('thursday') + getSleepHours('friday') + getSleepHours('saturday') + getSleepHours('saturday');

  
const getIdealSleepHours = idealHours => {
  idealHours = 7;
  return idealHours * 7;
}

const calculateSleepDebt = () => {
  const actualSleepHours = getActualSleepHours();
  const idealSleepHours = getIdealSleepHours();
  if(actualSleepHours === idealSleepHours) {
    console.log('You got the perfect amount of sleep!');
  } else if(actualSleepHours > idealSleepHours) {
    console.log('You got ' + (actualSleepHours - idealSleepHours) + ' hour(s) more sleep than needed.');
  } else {
    console.log('You got ' + (idealSleepHours - actualSleepHours) + ' hour(s) less sleep than you needed this week.  Get some rest.');
  }
}

console.log(calculateSleepDebt());

How many times do you call console.log? Presumably the undefined is one of them, so that’s where you’d be getting it from.

2 Likes

Thank you. It looks like I logged to the console on the final line, when I had already done so in the calculateSleepDebt function. I removed it, and it worked fine.

1 Like