Why do I get "Undefined" logged to the console when completing the Sleep Debt Calculator Exercise?

Here’s my code!:

function getSleepHours(day){

  if (day==="monday"){

    return 8;

  } else if(day==="tuesday"){

    return 7;

  } else if (day === "wednesday"){

    return 8;

  } else if (day === "thursday"){

    return 7;

  } else if (day === "friday"){

    return 8;

  } else if (day==="saturday"){

    return 7;

  } else if (day === "sunday"){

    return 8;

  }

}

getActualSleepHours=()=>{

  getSleepHours("monday");

  getSleepHours("tuesday");

  getSleepHours("wednesday");

  getSleepHours("thursday");

  getSleepHours("friday");

  getSleepHours("saturday");

  getSleepHours("sunday");

  gettot=getSleepHours("monday")+getSleepHours("tuesday")+getSleepHours("wednesday")+getSleepHours("thursday")+getSleepHours("friday")+getSleepHours("saturday")+getSleepHours("sunday");

  return gettot

}

getIdealSleepHours=()=>{

  idealHours= 8;

  return idealHours*7

}

calculateSleepDebt=()=>{

  actualSleepHours=getActualSleepHours()

  idealSleepHours=getIdealSleepHours()

  sleepdif1= actualSleepHours-idealSleepHours

  sleepdif2= idealSleepHours-actualSleepHours

  if (actualSleepHours===idealSleepHours){

    console.log("Your sleep is perfect");

  } else if (actualSleepHours>idealSleepHours){

    console.log("You're sleeping too much, about:"+sleepdif1+" hours too much...");

  } else if (actualSleepHours<idealSleepHours){

    console.log("You need to get a minimum of:"+sleepdif2+" more hours of sleep....");

  }

}

console.log(calculateSleepDebt())

It logs the correct number and string to the console but for some reason it also logs “undefined” underneath it, any help would be appreciated!!

calculateSleepDebt doesn’t return anything, yet when you call calculateSleepDebt function you log the returned result. What for?

Ahhh I get it now, thanks!
So basically whenever you’ve got a function that logs something to the console as one of its actions you shouldn’t embed it in another console.log() command otherwise you get undefined, so the reason why it would also output the correct answer and underneath it output undefined is because the function was technically called by the console.log()…?

by calling .log() method on your calculateSleepDebt function call, you log the returned result. But your calculateSleepDebt doesn’t return anything

so either using return in the function, or don’t bother attempting logging the result of the calculateSleepDebt call.

1 Like

Hi! I also struggle with similar problem. It returns with the “No Data” default value. I wrote a switch for calculateSleepDebt because it seemed more covinient for me. It runs, but doesn’t want to return with the case I want. Here is my code:

const getSleepHours = day => {

  switch (day) {

case 'monday': return 7;

break;

case 'tuesday': return 7;

break;

case 'wednesday': return 7;

break;

case 'thursday': return 7;

break;

case 'friday': return 8;

break;

case 'saturday': return 6;

break;

case 'sunday': return 8;

break;

default: return 'No data';

break;

  }

};

const getActualSleepHours = () =>

getSleepHours('monday') +

getSleepHours('tuesday') +

getSleepHours('wednesday') +

getSleepHours('thursday') +

getSleepHours('friday') +

getSleepHours('saturday') +

getSleepHours('sunday');

const getIdealSleepHours = () => {

const  idealHours = 8;

return idealHours * 7;

};

const calculateSleepDebt = () => {

  const actualSleepHours = getActualSleepHours();

  const idealSleepHours = getIdealSleepHours();

switch (actualSleepHours, idealSleepHours) {

  case actualSleepHours === idealSleepHours : 

  return 'Great, your sleeping habits are perfect';

  break;

  case actualSleepHours > idealSleepHours : 

  return 'You sleep more than you should!';

  break;

  case actualSleepHours < idealSleepHours : 

  return 'You should sleep more';

  break;

  default: return 'No data';

  break;

}

}

console.log(calculateSleepDebt());

The switch statement evaluates an expression, matching the expression’s value to a case clause, and executes statements associated with that case , as well as statements in case s that follow the matching case .

This is not the right place or time to use switch.

That looks fine, I’ve got something similar bar the breaks, which works. How are you calling getSleepHours?

Mine for reference:

const getSleepHours = day => {
  switch (day.toLowerCase()) {
    case 'monday': return 9;
    case 'tuesday': return 7;
    case 'wednesday': return 7;
    case 'thursday': return 8;
    case 'friday': return 9;
    case 'saturday': return 10;
    case 'sunday': return 7;
    default: return `${day} is not a valid day.`;
  }
}

Why are you asking this? geog provided this in his code?

Also, i think doing this:

default: return `${day} is not a valid day.`;

is a bad idea. Why? Because in getActualSleepHours, if you make a mistake when calling getSleepHours, you will get an error that JS can’t sum integers and string

which is a misleading, so ideally default should throw an exception. The program should not be allowed to continue when there is an invalid day.

Ah that’s my fault, I didn’t realise there was a scroll and could only see the first function.

Noted on the default case, I didn’t take that into account when summing up the hours.

Regarding the use of a switch statement, if you feel it’s not the right situation for a switch statement, you might want to take it up with the Codecademy team:

The function should accept a day as an argument and return the number of hours you slept that night.
For instance, if you got 8 hours of sleep on Monday night, calling getSleepHours('monday') should return 8 .
Use an if/else or switch statement to implement this.

we getting a bit side tracked here. But isn’t it something you should take into account? Because a string is returned, you might get a harder to trace error in getActualSleepHours

switch is the preferred approach for getSleepHours. I was talking about the switch used in calculateSleepDebt by geog92, where if, else if and else are a better fit

I said it was noted as in I’ll take that onboard and learn from it, I’m learning here, so I’m not going to know all of the facets of JS coding straight away. Anyways yeah we’re moving off topic here so I’m not going to reply around this any more.

@geog92 As @stetim94 mentioned, maybe a switch isn’t the most appropriate here but if you really want to use a switch statement it’ll need to be matched against true .

What I’ve picked up is that if there’s any operator expressions as your cases, like you’ve got here you have to compare it to true.

I understand, you are learning. That is why I mentioned it :wink: hopefully you will learn something from it :slight_smile: