Functions JavaScript


#1

https://www.codecademy.com/courses/learn-javascript-functions/projects/sleep-debt-calculator?course_redirect=introduction-to-javascript

LEARN JAVASCRIPT: FUNCTIONS
Sleep Debt Calculator

My function works fine but I see “undefined” under the output.
What’s wrong?

Code
const getSleepHours= day=>{
  day = day.toLowerCase();
  switch (day) {
    case "monday" :
      return 7;
    case "tuesday":
      return 6;
    case "wednesday":
      return 5;
    case "thursday":
      return 4;
    case "friday":
      return 7;
    case "saturday":
      return 6;
    case "sunday":
      return 7;
      break;
    default:
      return "TRASH";
                 }
};

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

const getIdealSleepHours=()=>{
const idealHours = 7;
return idealHours*7;
};
const calculateSleepDept=()=> { //not working
  const actualSleepHours = getActualSleepHours();
  const idealSleepHours = getIdealSleepHours();
  if (actualSleepHours === idealSleepHours){
    console.log("Perfect sleep");
  }else if (actualSleepHours > idealSleepHours){
     console.log("user got more sleep than needed.");
    }else  {
   console.log(`You got ${idealSleepHours - actualSleepHours} hours less than need. Please rest`);
    }
};
console.log(calculateSleepDept());
//console.log(getSleepHours("wednesday"));
//console.log(getActualSleepHours());
//console.log(getIdealSleepHours());

#2

console.log() is the source of that output, not your code. It can be ignored.

The reasoning is that the console is given a command and responds, but has no return value. Function and method calls made to the console always echo a response at termination.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.