Why is the output undefined?


#1

Currently finished the sleep debt calculator project here:

https://www.codecademy.com/courses/introduction-to-javascript/projects/sleep-debt-calculator

I did manage to get it working, but after it calculates the sleep debt, I also get an “undefined” output.

I was hoping someone could let me know why that is happening. thanks!

Here is my code:

let getSleepHours = day => {
  switch (day) {
          case 'Sunday':
          return 6
          case 'Monday':
          return 6
          case 'Tuesday':
          return 6
          case 'Wednesday':
          return 8
          case 'Thursday':
          return 7
          case 'Friday':
          return 8
          case 'Saturday':
          return 8
          default:
          return 'Invalid Day'
          }
}

const getActualSleepHours = () => 
  getSleepHours('Sunday') +
  getSleepHours('Monday') +
  getSleepHours('Tuesday') +
  getSleepHours('Wednesday') +
  getSleepHours('Thursday') +
  getSleepHours('Friday') +
  getSleepHours('Saturday') 

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

const calculateSleepDebt = () => {
  const actualSleepHours = getActualSleepHours()
  const idealSleepHours = getIdealSleepHours()
  const missingSleep = idealSleepHours - actualSleepHours
  const surplusSleep = actualSleepHours - idealSleepHours
  if (actualSleepHours === idealSleepHours) {
    console.log('You got the perfect amount of sleep')
  } else if (actualSleepHours < idealSleepHours) {
    console.log(`You need ${missingSleep} more  hours of sleep`)
  } else {
    console.log(`You got ${surplusSleep} more  hours of sleep than you needed`)
}
}

#2

i don’t get any output at all, did you call calculateSleepDebt?


#3

sorry, I forgot to add the last bit of code which is:

console.log(calculateSleepDebt())


#4

If you call console.log more times than you expect output then … I don’t need to say any more than that do I


#5

okay I think I follow. I called console.log within the function and out in the global scope, which was unnecessary. Correct?


#6

You’ll write something each time you call console.log. Whether something should happen or not is for the programmer, you, to decide. Correct is relative to your expectations.