Sleep debt calculator - Learn javascript


#1

hi there

Have a question about the following JS script. Why when i run the code does the first prompt say 'How many hours did you get on undefined?'

i'm not sure what I haven't declared for this to happen?

function getSleepHours (day) {
  var hours = prompt('How many hours of sleep did you get on ' + day + '?');
  return Number(hours);
}

function getActualSleepHours() {
  return getSleepHours('Monday') + getSleepHours('Tuesday') + getSleepHours('Wednesday') + getSleepHours('Thursday') + getSleepHours('Friday') + getSleepHours('Saturday') + getSleepHours('Sunday');
}

function getIdealSleepHours() {
  var idealHours = prompt('How much sleep would you prefer per night?');
	return Number(idealHours) * 7;
}

function calculateSleepDebt(day) {
  var actualSleepHours = getActualSleepHours();
  var idealSleepHours = getIdealSleepHours();
  
  if (actualSleepHours === idealSleepHours) {
  console.log('Perfect');
}

if (actualSleepHours < idealSleepHours) {
  console.log('Need more sleep');
}

if (actualSleepHours > idealSleepHours) {
  console.log('Get some rest');
}
  
}

getSleepHours();
calculateSleepDebt();

Thanks to anyone who can help!


#2

What happens if you group your return exoression so the entire thing is a single return value.

return (getSleepHours('Monday') + ... )

Another option is to refactor that line into a function of sorts.

function getActualSleepHours() {
    return (function() {
        var days = [ 'Monday', 'Tuesday', 'Wednesday',
            'Thursday', 'Friday', 'Saturday', 'Sunday' ];
        var hours = 0, day, i = 0;
        for ( ; i < 7; ) {
            day = days[i++];
            hours += getSleepHours(day);
        }
        return hours;
    })();
}

We bring your attention to the second last line, with () we invoke this IIEF so that its return value is passed to the outer function's caller.

Witness in this version where we do not invoke the function in the return value...

function getActualSleep_Hours() {
    return (function() {
        var days = [ 'Monday', 'Tuesday', 'Wednesday',
            'Thursday', 'Friday', 'Saturday', 'Sunday' ];
        var hours = 0, day, i = 0;
        for ( ; i < 7; ) {
            day = days[i++];
            hours += getSleepHours(day);
        }
        return hours;
    });
}
var sleep_hours = getActualSleep_Hours();

Now,

 > sleep_hours
=> [Function]
 > sleep_hours()
=> 44

Better we prefer the earlier version though since it gives back all of its memory. This latter approach does not so there is eventual memory leakage.


#3

I believe you are getting the 'undefined message' because the getSleepHours() function is expecting an argument for the 'day' parameter. I also believe that this exercise is unfinished as it doesn't tell you how to properly call the code to operate correctly and furthermore has an additional step on the end (step 10) that seems to want you to modify already working code with two variables that don't seem to be defined anywhere else (actualHoursPerWeek and idealHoursPerWeek.) Step 10 seems to do exactly the same thing that the program already does without modification. Regardless, you should be able to successfully run the program, by simply running calculateSleepDebt(); without the getSleepHours() function call.

Also, you have an unneeded parameter in your calculateSleepDebt() function. I don't believe it should have a 'day' parameter. :slight_smile:


#4

Since you're a mod, I feel like you should know this already, but if you're following along, the 'for' loops have not been covered yet and are inappropriate, imho, for this exercise.


#5

My mistake. That would explain the long drawn out addition. This is why we are constantly asking for links to exercises.


#6

I'm doing the same exercise right now, and i found that isn't necessary to call getSleepHours(); at the end.


#7

Hi Cherri - thanks for posting - it was helpful to solving the project. :slight_smile:


#8