Why cant I use these variables in my if else statement without getting undefined?

I cant seem to get this program to run when I use these variables for input? If I wanted to make this a running app with inputs from a text box or something how could I use variables so that the user could actually input the number of hours for each day?

let monday_input = 0

let tuesday_input = 0

let wednesday_input = 0

let thursday_input = 0

let friday_input = 0

let saturday_input = 0

let sunday_input = 0

  const getSleepHours = (day) => {

    if (day === 'monday') {

      return monday_input;   

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

      return tuesday_input;

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

      return wednesday_input;

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

      return thursday_input;

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

      return friday_input;

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

      return saturday_input;

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

      return sunday_input;

    }else{

      return 'Error:this is not a valid day of the week.';

    } 

 };

  const getActualSleepHours = () => {

getSleepHours('monday') + getSleepHours('tuesday') + getSleepHours('wednesday') + getSleepHours('thursday') + getSleepHours('friday') + getSleepHours('saturday') + getSleepHours('sunday'); 

}

  const getIdealSleepHours = () => {

   let idealHours = 8.5

   return idealHours * 7;

}

  const calculateSleepDebt = () =>{

   let sleepDebt = getIdealSleepHours() - getActualSleepHours()

    if (sleepDebt = 0) {

      console.log('You have acquired adequate rest.')

    }else if (sleepDebt < 0) {

      console.log('You have acquired more than adequate rest.')

    } else {

      console.log('You need more rest!')

    }   

    if (getActualSleepHours < getIdealSleepHours) {

  console.log('You got ' + (idealSleepHours - actualSleepHours) + ' hour(s) less sleep than you should have this week. Get to bed!');

 }

}

calculateSleepDebt();

calculateSleepDebt();

Hello.

There are some mistakes and typos in your code.

  1. In function getActualSleepHours you must use return statement. Without return statement this function always returns undefined.
const getActualSleepHours = () => {
    return getSleepHours('monday') + 
        getSleepHours('tuesday') + 
        getSleepHours('wednesday') + 
        getSleepHours('thursday') + 
        getSleepHours('friday') + 
        getSleepHours('saturday') +
        getSleepHours('sunday');
}

Or you can remove curly brackets instead of using return:

const getActualSleepHours = () => getSleepHours('monday') + 
        getSleepHours('tuesday') + 
        getSleepHours('wednesday') + 
        getSleepHours('thursday') + 
        getSleepHours('friday') + 
        getSleepHours('saturday') +
        getSleepHours('sunday');
  1. And some mistakes in calculateSleepDebt function:

2.1. In first condition you use assignment operator instead of compare: if (sleepDebt = 0) must be if (sleepDebt === 0). With assignment operator you set value of sleepDebt always to zero.

2.2. In second condition you use function names without brackets: if (getActualSleepHours < getIdealSleepHours) instead of if (getActualSleepHours() < getIdealSleepHours()). So you compare functions itself instead of function invocation results.

2.3. And in last log you use undefined variables idealSleepHours - actualSleepHours. I recommend to define these variables at the begining of function and use it instead of function invocations:

const idealSleepHours = getIdealSleepHours();
const actualSleepHours = getActualSleepHours();

And small advice: do not forget to use semicolons at the end of statements.

I do not provide fixed code, so you can fix it by yourself. But if you wish, I can post it here.

PS Sorry for my English)

1 Like