Cant figure it out


#1

function getSleepHours(day){
if (day === ‘monday’){
return 7.5;
} else if (day === ‘tuesday’){
return 7.5;
} else if (day === ‘wednesday’){
return 7.5;
} else if(day === ‘thursday’){
return 7.5;
} else if (day === ‘friday’){
return 7.5;
} else if (day === ‘satuday’){
return 7.5;
} else if (day === ‘sunday’){
return 7.5;
}
}

function getActualSleepHours (){
return getSleepHours(‘monday’) +
getSleepHours(‘tuesday’) +
getSleepHours(‘wednesday’) +
getSleepHours(‘thursday’) +
getSleepHours(‘friday’) +
getSleepHours(‘saturday’) +
getSleepHours(‘sunday’)
}

function getIdealSleepHours(){
var idealHours = 7.5;
return idealHours * 7;
}

function calculateSleepDebt(){
var ActualSleepHours = getActualSleepHours();
var idealSleepHours = getIdealSleepHours();

if (ActualSleepHours === idealSleepHours) {
console.log(‘You got the perfect amount of sleep’)
}
if (ActualSleepHours < idealSleepHours) {
console.log(‘you need more sleep, go to bed’)
}
if (ActualSleepHours > idealSleepHours){
console.log(‘You got more sleep then needed’)
}
}

calculateSleepDebt()

so that;s my code and i cant seem to figure out where i am going wrong, i am getting no errors it just simply wont run. any help would be greatly appreciated.


#2

Is this the link to the exercise?

https://www.codecademy.com/courses/learn-javascript/projects/sleep-debt-calculator


A switch is much less verbose…

  switch (day) {
    case 'Sunday': return 7;
    case 'Monday': return 7;
    case 'Tuesday': return 7;
    case 'Wednesday': return 7;
    case 'Thursday': return 7;
    case 'Friday': return 8;
    case 'Saturday': return 6;
  }

For brevity and simplicity, we can define a list of days that remains constant and accessible to all our funcitons.

// a top of code
const days = [
    "Sunday", "Monday", "Tuesday",
    "Wednesday", "Thursday",
    "Friday", "Saturday"
];

Now in our function,

function getSleepHours(day) {
  const hours = [5.5,6,6.5,7,8,7.5,8.5];
  return hours[days.indexOf(day)];
}

Still playing with this newly revised lesson. All older topics on this project will be out of date since the prompt() for user input has been removed from the sandbox. We have to completely redo our old code.


#3

Again, using a const lookup table keeps everything consistent with fewer potential typos or bugs.

function getActualSleepHours() {
  let total = 0;
  for (let i = 0; i < days.length; i++) {
    total += getSleepHours(days[i]);
  }
  return total;
}

Note that above is using ES6+ syntax which is supported, now.

function getIdealSleepHours() {
  let idealHours = 7.5;
  return idealHours * 7;
}

If you’re really daring, try using an arrow function…

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

and the full blown pure function…

const getIdealSleepHours = (idealHours = 7.5) => idealHours * 7;

If you are just getting your feet wet in JavaScript, then polish your code to the ES5 standard you have so far learned. But leaked into that track there are now hints of ES6 and above which most browsers now support (even up to ES8!).

So the challenge in the project can be to write it in ES6+ syntax…

const days = [
  "Sunday", "Monday", "Tuesday",
  "Wednesday", "Thursday",
  "Friday", "Saturday"
];
const hours = [5.5, 6, 6.5, 7, 8, 7.5, 8.5];

const getSleepHours = (day) => hours[days.indexOf(day)];

const getActualSleepHours = () => hours.reduce((a, b) => a + b);

const getIdealSleepHours = (idealHours = 7.5) => idealHours * 7;

const sleepDebtCalculator = () => {
  const difference = getIdealSleepHours() - getActualSleepHours();
  console.log(
    difference < 0 ? `${difference} more hours sleep than ideal` : 
    difference > 0 ? `${difference} less hours sleep than ideal` : 
    'Ideal amount'
  );
};

Note that since there is no user input, the hours array is a constant and we don’t even call the getSleepHours function. It will still be useful though.

const sleepDebtCalculator = () => {  
  for (const day of days) console.log(day, getSleepHours(day));
  const difference = getIdealSleepHours() - getActualSleepHours();
  console.log(
    difference < 0 ? `${difference} more hours sleep than ideal` : 
    difference > 0 ? `${difference} less hours sleep than ideal` : 
    'Ideal amount'
  );
};