Hi, I managed to get on okay with this project and all of the code works and adjusts when I change the daily hours. However, it always gives me an undefined at the end of the console no matter what output. Why is this undefined here?
// creating a function that takes how many hours I slept each day of the week
const getSleepHours = day => {
day = day.toLowerCase();
if (day === ‘monday’) {
return 9;
} else if (day === ‘tuesday’) {
return 4;
} else if (day === ‘wednesday’) {
return 13;
} else if (day === ‘thursday’) {
return 7;
} else if (day === ‘friday’) {
return 8;
} else if (day === ‘saturday’) {
return 8;
} else if (day === ‘sunday’) {
return 7;
}
};
// tests the function works
console.log(getSleepHours(‘SATURDAY’));
/* creating a function that takes how many hours I slept each day and calculates it in hours/week */
const getActualSleepHours = () => {
return (getSleepHours(‘monday’) + getSleepHours(‘tuesday’) + getSleepHours(‘wednesday’) + getSleepHours(‘thursday’) + getSleepHours(‘friday’) + getSleepHours (‘saturday’) + getSleepHours(‘sunday’));
};
// tests function works
console.log(getActualSleepHours());
// creating function of how many hours I want per night into hours/week
const getIdealSleepHours = () => {
let idealHours = 8;
return idealHours * 7;
}
// tests function works
console.log(getIdealSleepHours());
// creating function to calculate sleep debt
const calculateSleepDebt = () => {
// creates variables relating to the functions to be used in this function
let actualSleepHours = getActualSleepHours();
let idealSleepHours = getIdealSleepHours();
// if/else that reacts based on actualSleepHours compared to idealSleepHours
if (actualSleepHours === idealSleepHours){
console.log(‘You got the perfect amount of sleep!’);
} else if (actualSleepHours > idealSleepHours){
console.log(‘You got ’ + (actualSleepHours - idealSleepHours) + ’ hours more than needed!’);
} else if(actualSleepHours < idealSleepHours){
console.log(‘You need to get more sleep! You got ’ + (idealSleepHours - actualSleepHours) + ’ less than needed!’);
}
};
// calls final function to calculate sleep debt and give advice
console.log(calculateSleepDebt());
Console returns:
8
56
56
You got the perfect amount of sleep!
undefined