sleepDebtCalculator

const getSleepHours = day => {
  switch (day) {
    case 'monday' :
      return 8;
    case 'tuesday' :
      return 7;
    case 'wednesday' :
      return 7;
    case 'thursday' :
      return 6;
    case 'friday' :
      return 5;
    case 'saturday' :
      return 5;
    case 'sunday' :
      return 8;
  }
};

const getActualSleepHours = () =>  getSleepHours('monday') + getSleepHours('tuesday') + getSleepHours('wednesday') + getSleepHours('thursday') + getSleepHours('friday') + getSleepHours('saturday') + getSleepHours('sunday');

const getIdealSleepHours = () => {
  let idealHours = 6;
  return console.log(`ideal sleep is ${idealHours * 7} hours`);
};

 const calculateSleepDebt = () => {
    const actualSleepHours = console.log(getActualSleepHours());
    const idealSleepHours = getIdealSleepHours();

    if (actualSleepHours === idealSleepHours) {
      console.log('User got perfect amount of sleep with perfect hours of ' + actualSleepHours);
    } else if (actualSleepHours > idealSleepHours) {
      console.log('User got more sleep than needed with ' + (actualSleepHours - idealSleepHours) + 'hours');
    } else if (actualSleepHours < idealSleepHours) {
      console.log('User should get some rest less hours of sleep with '+ (idealSleepHours - actualSleepHours) + 'hours');
    }

 };

calculateSleepDebt();

output =

46
ideal sleep is 42 hours
User got perfect amount of sleep with perfect hours of undefined

when i put any hours in idealHours, it gives same output for every hours as above. why ?

Hi, first thing I’m noticing is:

const actualSleepHours = console.log(getActualSleepHours());

Instead of assigning the value of getActualSleepHours() to actualSleepHours, You’re just logging it to the console.

that’s why the output first prints 46 (the output of console.log(getActualSleepHours()) and in the end it’s undefined; because that’s the value of actualSleepHours so the if block is not working correctly.

Omitting the console.log from the assignment of actualSleepHours should fix this problem.

2 Likes

Thanks @terasolver66644 , i found the solution, removed console.log from idealSleepHours , still struggling with return statement :pensive:

Did your try putting idealHours as a parameter to the function?

sorry, i didn’t get it,
but i declared it using let only, haven’t tried assigning through parameter.

I can’t find the problem, but I would try to put idealHours as a parameter to the function, like this:

const getIdealSleepHours = (idealHours) => {
  return console.log(`ideal sleep is ${idealHours * 7} hours`);
};

And then calling it with the value:

const idealSleepHours = getIdealSleepHours(6);

And maybe it’ll work, or at least guide you towards a solution.

I think I found the problem:

const getIdealSleepHours = () => {
  let idealHours = 6;
  return console.log(`ideal sleep is ${idealHours * 7} hours`);
};

You don’t need to return a console.log statement. You can omit the return and it’ll still log to the console when the function is called.

But you do need to return the value of idealHours times 7. this function currently only prints to the console and doesn’t return anything. I would change it to something like this:

const getIdealSleepHours = () => {
  let idealHours = 6;
  let weeklyIdealHours = idealHours * 7;
  console.log(`ideal sleep is ${weeklyIdealHours} hours`);
  return weeklyIdealHours;
};

And then const idealSleepHours = getIdealSleepHours(); in the calculateSleepDebt() function should work as intended.

1 Like