Unfinished Project?


#1

I find this project very confusing. It seems that steps are skipped multiple times. Here is my code so far, and I continue you to get:


ReferenceError: actualSleepHours is not defined

Code:

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 many hours of sleeep would you like to get each night?');
  return Number(idealHours) * 7;
}

function calculateSleepDebt() {
  var actualSleepHours = getActualSleepHours();
  var idealSleepHours = getIdealSleepHours();
  if (actualSleepHours === idealSleepHours) {
    console.log("You are getting the perfect amount of sleep");
  }
  if (actualSleepHours > idealSleepHours)
    console.log('You got ' + (actualSleepHours - idealSleepHours) + ' hours more sleep than your needed this week.');
  }
  if (actualSleepHours < idealSleepHours) {
    console.log('You got ' + (idealSleepHours - actualSleepHours) + ' hours less sleep than you needed this week. You should try to sleep more.');
  }

getSleepHours ();
calculateSleepDebt ();

#2

Has no one answered this yet?


#3

You are probably going to pull your hair out when you read this (I make these mistakes ALL the time) :persevere:....
You are missing an opening curly brace in your second if statement and you need to close out the calculateSleepDebt() function with a final curly brace after you last if statement. Also I don't believe you need to call the getSleepHours() function at the end as it is called within the calculateSleepDept() function. I ran your code with those two fixes and calling only the calculateSleepDebt() function and it executed without error.


#4

This might help. First off you can’t use “prompt”. Although it is a javascript built in function it isn’t built into every exercise. Second you never give a number for actual sleep hours.

Here is my code. It works except for when you sleep to much. I am still working on that.

const getSleepHours = day => {
switch(day) {
case ‘Sunday’:
return 9;
break;
case ‘Monday’:
return 8;
break;
case ‘Tuesday’:
return 8;
break;
case ‘Wednesday’:
return 9;
break;
case ‘Thursday’:
return 8;
break;
case ‘Friday’:
return 8;
break;
case ‘Saturday’:
return 8;
break;
};
};

const getActualSleepHours = () => {
return getSleepHours(‘Sunday’) +
getSleepHours(‘Monday’) +
getSleepHours(‘Tuesday’) +
getSleepHours(‘Wednesday’) +
getSleepHours(‘Thursday’) +
getSleepHours(‘Friday’) +
getSleepHours(‘Saturday’);
};

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

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

if (actualSleepHours === idealSleepHours) {
console.log(‘You got the perfect amount of sleep.’);
} if (actualSleepHours > idealSleepHours) {
console.log(‘You got more sleep than needed.’);
} if (actualSleepHours < idealSleepHours) {
console.log(‘You should get more sleep.’)
}
if (actualSleepHours < idealSleepHours) {
console.log(‘You got ’ + (idealSleepHours - actualSleepHours) + ’ hours less sleep than you needed this week. Get some rest.’);
}
if (actualSleepHours > 56) {
console.log('You got ’ + (idealSleepHours + actualSleepHours - 56) + " hours more sleep than you needed this week. Don’t sleep so much")
}
};

calculateSleepDebt()