Sleep debt calculator


#1
const getSleepHours = (day) =>{
 if (day === "Monday") {
   return 3;
 }else if (day === "Tuesday"){
   return 4;
 }
};

const getActualSleepHours = () =>{
getSleepHours('Monday') + getSleepHours('Tuesday') 
};

console.log(getActualSleepHours());

Hi just wondering where I am going wrong here, I’m sure it is something small but it has been bugging me that it keeps returning “undefined”


#2

here:

console.log(getActualSleepHours());

you log the returned value, which is undefined because of absence of a return keyword in your function


#3
const getSleepHours = (day) =>{
 if (day === "Monday") {
   return 2;
 }else if (day === "Tuesday"){
   return 4;
 }
};

const getActualSleepHours = () =>{
return getSleepHours('Monday') + getSleepHours('Tuesday') 
};

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

const calculateSleepDebt = () =>{
  const actualSleepHours = getActualSleepHours();
  const idealSleepHours = getIdealSleepHours();
  if (actualSleepHours === idealSleepHours){
    console.log("A good week of sleep G")
  }else if(actualSleepHours < idealSleepHours){
    console.log("guy you no try at all sleep more!")
  }else{
    console.log("Are you on vacation!?")
  };
}

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

Thanks but I have the return keyword but it still returns undefined?


#4

same problem but for a different function?


#5

I am not quite sure where the problem is. I have looked through the code but do not understand why I am getting “Undefined” as an output.


#6

only this time, its caused by the calculateSleepDebt function.


#7

so where is this return keyword going to go?


#8

do you want a return keyword? You could also consider not logging anything if it doesn’t add value (like undefined)


#9

basically I just want to know exactly what to do to run this code without it logging “undefined”?


#10

Find which console.log call is pritning that, and then look at what you did there. This should be very straightforward.

When you don’t get the result you expected, look at how you got it.