Advertisement
makispaiktis

Codecademy - 11th Exercise (Sleep Debt Calculator)

Oct 2nd, 2019 (edited)
509
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // 1st Function - Determines the sleep Hours per day
  2. function getSleepHours(day){
  3.     // I will suppose that the hours of sleeping
  4.   // each day of a week is stable for every
  5.   // single week and I am the one that decides
  6.   // for that with specific numbers with this       // function
  7.   switch(day){
  8.     case "Monday":
  9.       return 8;
  10.       break;
  11.     case "Tuesday":
  12.       return 7;
  13.       break;
  14.     case "Wednesday":
  15.       return 5;
  16.       break;
  17.     case "Thursday":
  18.       return 6;
  19.       break;
  20.     case "Friday":
  21.       return 8;
  22.       break;
  23.     case "Saturday":
  24.       return 6;
  25.       break;
  26.     case "Sunday":
  27.       return 10;
  28.       break;
  29.     default:
  30.       console.log(day + " is not a name of a week's day...");
  31.   } // End of switch
  32.  
  33. }
  34.  
  35.  
  36. // 2nd Function - Calculates the actual hours of sleeping per week
  37. // Concise way of declaring a function
  38. const getActualSleepHours = () => { return getSleepHours("Monday") +
  39. getSleepHours("Tuesday") + getSleepHours("Wednesday") + getSleepHours("Thursday") + getSleepHours("Friday") + getSleepHours("Saturday") + getSleepHours("Sunday")
  40. };
  41.  
  42.  
  43. // 3rd Function - Calculates the ideal hours of sleeping per week
  44. function getIdealSleepHours(){
  45.   const idealHours = 8;
  46.   return 7 * idealHours;
  47. }
  48.  
  49.  
  50. // 4th Function - Calculates the sleep Debt
  51. function calculateSleepDebt(){
  52.   actualSleepHours = getActualSleepHours();
  53.   idealSleepHours = getIdealSleepHours();
  54.   // If-else-cases
  55.   if(actualSleepHours === idealSleepHours){
  56.     console.log("You got the perfect amount of sleeping this week (" + getIdealSleepHours() + ").");
  57.   }
  58.   else if(actualSleepHours > idealSleepHours){
  59.     console.log("You slept " + actualSleepHours + " hours. It's too much for this week.");
  60.     console.log("You got " + (actualSleepHours-idealSleepHours) + " more than the normal " + getIdealSleepHours() + " hours per week.");
  61.   }
  62.   else{
  63.     console.log("This week you slept " + actualSleepHours + " hours. It's less than the ideal sleep hours needed.");
  64.     console.log("You got " + (idealSleepHours-actualSleepHours) + " less than the normal " + getIdealSleepHours() + " hours per week.");
  65.   }
  66. }
  67.  
  68.  
  69. // Main part of the project
  70. calculateSleepDebt();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement