Sleep Debt Calculator

Hi Guys, Sorry to bother you again with another exercise question , but im stuck. I get undefined on the getActualSleepHours function…

What am i doing wrong ?

// Its a Function to determine how many hours of sleep you got each night of the week.
const getSleepHours = day => {
  day = day.toLowerCase();
if (day === 'monday') {
  return 8;}
else if (day === 'tuesday') {
  return 7;}
  else if (day === 'wednesday') {
  return 6;}
  else if (day === 'thrusday') {
  return 7;}
  else if (day === 'friday') {
  return 8;}
  else if (day === 'saturday') {
  return 9;}
 else if (day === 'sunday') {
  return 7;}
};

console.log(getSleepHours('Tuesday'));
//It gets the total number of hours you slept
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();};


console.log(getIdealSleepHours());

const caculateSleepDebt = () => {
const actualSleepHours = getActualSleepHours();
const idealSleepHours = getIdealSleepHours();
  if (actualSleepHours === idealSleepHours) {
console.log('You have got the perfect amount of sleep.');}
    if (actualSleepHours > idealSleepHours) {
console.log('You got ' + (idealSleepHours - actualSleepHours) + ' hour(s) less sleep than you needed this week. Get some rest.');};
    if (actualSleepHours < idealSleepHours) {
console.log('You got ' + (idealSleepHours - actualSleepHours) + ' hour(s) more than you needed this week. Well Done.');};
};

calculateSleepDebt();

console.log(calculateSleepDebt());







1 Like

Should this function be returning something?

1 Like

Yes, it should return the total Sleep Hours calculated.

When we use curly braces in arrow function syntax there is no implicit return. Modify your function as follows:

 return getSleepHours('monday') + ...

so that the sum computed is the value returned.

1 Like

do you mean like this ?

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

console.log(getActualSleepHours());

Not quite. Go back to your original code and just insert the return keyword on the first line. You will not want that last line as it will raise an error, Just delete it and follow the example in my last post.

2 Likes

ok , thanks , just what i thought. Anyway i still cannot get anything at the end.

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

//console.log(getActualSleepHours());  <---- This give me NaN

At the end i start the program but i get nothing .

// Its a Function to determine how many hours of sleep you got each night of the week.
const getSleepHours = day => {
 day = day.toLowerCase();
if (day === 'monday') {
  return 8;}
else if (day === 'tuesday') {
  return 7;}
  else if (day === 'wednesday') {
  return 6;}
  else if (day === 'thrusday') {
  return 7;}
  else if (day === 'friday') {
  return 8;}
  else if (day === 'saturday') {
  return 9;}
 else if (day === 'sunday') {
  return 7;}
};

//console.log(getSleepHours('Tuesday'));
//It gets the total number of hours you slept
const getActualSleepHours = () => {
      return getSleepHours('monday') +
      getSleepHours('tuesday') +
      getSleepHours('wednesday') +
      getSleepHours('thursday') +
      getSleepHours('friday') +
      getSleepHours('saturday') +
      getSleepHours('sunday');}

//console.log(getActualSleepHours());

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

const calculateSleepDebt = () => {
const actualSleepHours = getActualSleepHours();
  const idealSleepHours = getIdealSleepHours();};


//console.log(getIdealSleepHours());

const caculateSleepDebt = () => {
const actualSleepHours = getActualSleepHours();
const idealSleepHours = getIdealSleepHours();
  if (actualSleepHours === idealSleepHours) {
console.log('You have got the perfect amount of sleep.');}
if (actualSleepHours > idealSleepHours) {
console.log('You got ' + (idealSleepHours - actualSleepHours) + ' hour(s) less sleep than you needed this week. Get some rest.');};
if (actualSleepHours < idealSleepHours) {
console.log('You got ' + (idealSleepHours - actualSleepHours) + ' hour(s) more than you needed this week. Well Done.');};
};

calculateSleepDebt();

//console.log(calculateSleepDebt());







You have a calculateSleepDebt() and a caculateSleepDebt() function. You’re calling the first one at the end of your program.

4 Likes

Which day of the week is ‘thrusday’? :wink:

4 Likes

:sweat_smile: , Thanks ¡

I think doublechecking for typos its another part of coding .

Ill have a look to the next steps .

Thanks a lot ¡

2 Likes

Hello.
Can you please help me with this part of code?
const calculateSleepDebt = () => {
let actualSleepHours = getActualSleepHours();
let idealSleepHours = getIdealSleepHours ();
};

console.log(actualSleepHours())

if (actualSleepHours === idealSleepHours){
console.log(‘Perfect amount of sleep’);
} else if(actualSleepHours > idealSleepHours){
console.log(‘You got more sleep than needed’);
} else {
console.log(‘You should get some rest’);
}
calculateSleepDebt();

the error is Avatar

/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/sleepDebtCalculator.js:32 console.log(actualSleepHours()) ^ ReferenceError: actualSleepHours is not defined at Object.
Can’t find out the reason
full code

Hello, @rinajalalyan43849927, and welcome to the forums.

The error message is telling you that something is not defined. Is it defined? If so, where? Don’t forget that a variable declared inside a function is limited in scope to that function. Perhaps there is code that was intended to be inside a function, but isn’t?

1 Like

Hello, thank you for your help!
Yes, I got it! I declared actualSleepHours in the function, but than used it outside of the function.
Thank you too much, now the project is working !

2 Likes

The variable is defined within a function so not in global scope.

1 Like

Hello,
Thanks for any help unfurling why instead of summing up my sleep hours, the

const getActualSleepHours = () =>

just appears to be making up a string of the respective daily hours of sleep for Monday, Tuesday, Wednesday etc, etc under my:

const getSleepHours = day => {

I’m clearly off course here not getting the numbers to sum up, but I can’t square up where I’ve deviated from the video walk through up to this stage.
Regards.

const getSleepHours = day => {
if (day === 'Monday') {
  return '8';
}
 else if (day === 'Tuesday') {
  return '7';
} 
else if (day === 'Wednesday') {
  return '8';
} 
else if (day === 'Thursday') {
  return '8';
} 
else if (day === 'Friday') {
  return '5';
} 
else if (day === 'Saturday') {
  return '8';
} else  {
  console.log('error')

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

  console.log(getSleepHours('Wednesday'));
console.log(getActualSleepHours());

Try this:

console.log(5 + 5);
console.log('5' + '5')

Notice a difference?

Thanks for that insight. I realize I had my getSleepHours return in a string, not as a number itself. My eyes were telling me I was in alignment with the steps in the video and others on this thread, alas I fell victim to expectation bias.

For future education, is this a correct or proper way to ensure any other value (I probably have the parlance wrong) besides Monday-Sunday returns zero?

} else {
return 0;

Clearly in code I sent you yesterday the

else {
console.log(‘error’)

…was outputting error and NaN when running the getActualSleepHours function. Once modified to return zero, all checked out.
Thanks again. Or until the next mental road block emerges :neutral_face:

const getSleepHours = day => {
if (day === 'Monday') {
  return 8;
}
 else if (day === 'Tuesday') {
  return 7;
} 
else if (day === 'Wednesday') {
  return 8;
} 
else if (day === 'Thursday') {
  return 4;
} 
else if (day === 'Friday') {
  return 5;
} 
else if (day === 'Saturday') {
  return 8;
} else  {
  return 0;


}
 
}
console.log(getSleepHours('Hey there '));

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

console.log(getActualSleepHours());

The behavior for input other than Monday - Sunday is kind of up to you. Would you rather get 0 back, and not interrupt the user experience, or would you rather get NaN as the final output to alert the user that something is wrong? Personally, I would probably opt for something like:

} else { // Now we can see that something was wrong, and what the input was that caused it.
  console.log(`Sorry. There is no such day as ${day}.`);
  // We also return something that will allow the program to provide output for the input that was correct.
  return 0;
}

Consider, however, why you received NaN. Did you include input other than Monday through Sunday? I can point you right at what happened, or I can show you how to find it. I’d rather you show you how to debug the code yourself. Try this:

//comment out everything inside the function except for one call to getSleepHours()
//do this so you can observe what you get back for each value (day)
//so first:
const getActualSleepHours = () => 
  getSleepHours('Monday') //+
  //getSleepHours('Tuesday') +
  //getSleepHours('Wednesday') +
  //getSleepHours('Thursday') +
  //getSleepHours('Friday') +
  //getSleepHours('Saturday') +
  //getSleepHours('Sunday');

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

//continue until you've executed your program once for each individual input
//make sure to observe the output

Thanks @midlindner for nicely clearing up my questions. I was able to complete the project then I circled back to apply the commenting out procedures as you specified. I noticed no errors, but I by then I had cleaned up the

else {
console.log(‘error’) //to your suggested
else {
console.log(‘that day doesnt exist’);
return 0;
}
}

and thus because of that the NaN never recurred.

Thanks again for all your gracious wisdom.

The reason I wanted you to try each day separately was because your original code (posted below) did not include ‘Sunday’. That’s why when you ran your code you received NaN as the final output.
Your getActualSleepHours function was returning 8 + 7 + 8 + 8 + 5 + 8 + undefined which evaluates to NaN. Since ‘Sunday’ was missing, your code logged error to the console, and implicitly returned undefined. When you changed your code to return 0; instead of console.log('error'), you got 0 in place of undefined when you tried to get the hours for ‘Sunday’. At some point, you obviously noticed that ‘Sunday’ was missing, and added it. :slightly_smiling_face: