Advertisement
makispaiktis

2. Codecademy Intro - Sleep Dedt

Oct 17th, 2024 (edited)
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // 1. Map every day with the sleep hours I got
  2. function getSleepHours(day){
  3.     switch(day.toLowerCase()){
  4.         case "monday":
  5.             return 8;
  6.         case "tuesday":
  7.             return 7;
  8.         case "wednesday":
  9.             return 8;
  10.         case "thursday":
  11.             return 6.5;
  12.         case "friday":
  13.             return 7;
  14.         case "saturday":
  15.             return 7.5;
  16.         case "sunday":
  17.             return 8.5;
  18.     }
  19. }
  20.  
  21. // 2. Total sleep hours
  22. const getActualSleepHours = () => {
  23.     let sum = 0;
  24.     for(let i=0; i<days.length; i++){
  25.         sum += getSleepHours(days[i]);
  26.     }
  27.     return sum;
  28. }
  29.  
  30. // 3. Total ideal hours
  31. const getIdealSleeplHours = idealHours => 7 * idealHours;
  32.  
  33. // 4. Sleep Debt
  34. function calculateSleepDebt(idealHours){
  35.     actual = getActualSleepHours();
  36.     ideal = getIdealSleeplHours(idealHours);
  37.     if(actual === ideal){
  38.         console.log('You took the perfect amount of sleep this week!');
  39.     }
  40.     else if(actual > ideal){
  41.         console.log(`You got more sleep (${actual - ideal} hours more) this week than needed!`);
  42.     }
  43.     else{
  44.         console.log(`You need some sleep! You slept ${ideal - actual} hours less this week!`);
  45.     }
  46. }
  47.  
  48. // MAIN FUNCTION
  49. days = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"];
  50. idealHours = 8;
  51. calculateSleepDebt(idealHours);
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement