Skip to content

Instantly share code, notes, and snippets.

@RaynZayd
Last active October 24, 2020 11:32
Show Gist options
  • Save RaynZayd/42d0be21be983d5011c51366ff646289 to your computer and use it in GitHub Desktop.
Save RaynZayd/42d0be21be983d5011c51366ff646289 to your computer and use it in GitHub Desktop.
This project calculates if you’re getting enough sleep each week using a sleep debt calculator. The program determines actual and ideal hours of sleep for each night of the last week. It also calculates, in hours, how far you are from your weekly sleep goal.
/*Sleep Debt Calculator*/
/*This project calculates if you’re getting enough sleep each week using a sleep debt calculator.
The program determines actual and ideal hours of sleep for each night of the last week.
It also calculates, in hours, how far you are from your weekly sleep goal.*/
//Determins how many hours of sleep you got each night of the week.
const getSleepHours = day => {
if(day === 'monday'){
return 8;
}else if(day === 'tuesday'){
return 8;
}else if(day === 'wednesday'){
return 10;
}else if(day === 'thursday'){
return 8;
}else if(day === 'friday'){
return 8;
}else if(day === 'saturday'){
return 9;
}else if(day === 'sunday'){
return 8;
}
}
//Test function by calling it multiple times
//console.log(getSleepHours('monday'));
//console.log(getSleepHours('wednesday'));
//console.log(getSleepHours('friday'));
//Total sleep hours that you actually slept
const getActualSleepHours = () => {
return getSleepHours('monday') +
getSleepHours('tuesday') +
getSleepHours('wednesday')+
getSleepHours('thursday')+
getSleepHours('friday')+
getSleepHours('saturday')+
getSleepHours('sunday');
}
//Ideal sleep hours preferred
const getIdealSleepHours = () => {
var idealHours = 8;//Ideal hours per night
return idealHours * 7//Total hours prefered per week
}
//Testing functions
//console.log(getIdealSleepHours());
//console.log(getIdealSleepHours());
//Calculates sleep debt
const calculateSleepDebt = () => {
actualSleepHours = getActualSleepHours();
idealSleepHours = getIdealSleepHours();
if(actualSleepHours === idealSleepHours){
console.log(`You had the perfect amount of sleep. \n Over Ideal Sleep: ${actualSleepHours - idealSleepHours} \n Under Ideal Sleep: ${idealSleepHours - actualSleepHours}`);
}else if(actualSleepHours > idealSleepHours){
console.log(`You slept more than needed. \n Over Ideal Sleep: ${actualSleepHours - idealSleepHours} \n Under Ideal Sleep: ${idealSleepHours - actualSleepHours}`);
}else if(actualSleepHours < idealSleepHours){
console.log(`You should get some rest. \n Over Ideal Sleep: ${actualSleepHours - idealSleepHours} \n Under Ideal Sleep: ${idealSleepHours - actualSleepHours}`);
}else{
console.log('Unknown!');
}
}
calculateSleepDebt();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment