Advertisement
makispaiktis

3. Codecademy Intro - Training Days

Oct 17th, 2024
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const getRandEvent = () => {
  2.     const random = Math.floor(Math.random() * 3);
  3.     if (random === 0) {
  4.       return 'Marathon';
  5.     } else if (random === 1) {
  6.       return 'Triathlon';
  7.     } else if (random === 2) {
  8.       return 'Pentathlon';
  9.     }
  10. };
  11.  
  12. // The scope of `days` is too tight
  13. const getTrainingDays = event => {
  14.   let days;
  15.   if (event === 'Marathon') {
  16.     days = 50;
  17.   }
  18.   else if (event === 'Triathlon') {
  19.     days = 100;
  20.   }
  21.   else if (event === 'Pentathlon') {
  22.     days = 200;
  23.   }
  24.   return days;
  25. };
  26.  
  27. // The scope of `name` is too tight
  28. const logEvent = (name, event) => {
  29.   console.log(`${name}'s event is: ${event}`);
  30. };
  31.  
  32. const logTime = (name, days) => {
  33.  console.log(`${name}'s time to train is: ${days} days`);
  34. };
  35.  
  36.  
  37. // MAIN FUNCTION
  38. const name = 'Nala';
  39. const event = getRandEvent();
  40. const days = getTrainingDays(event);
  41. logEvent(name, event);
  42. logTime(name, days);
  43. console.log();
  44. const event2 = getRandEvent();
  45. const days2 = getTrainingDays(event2);
  46. const name2 = 'Warren';
  47. logEvent(name2, event2);
  48. logTime(name2, days2);
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement