Advertisement
3vo

Problem 11. Beer Time

3vo
Oct 25th, 2022
737
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // A beer time is after 1:00 PM and before 3:00 AM.
  2. // Write a program that enters a time in format “hh:mm tt” (an hour in range [01...12],
  3. // a minute in range [00…59] and AM / PM designator) and prints beer time or non-beer time according to
  4. // the definition above or invalid time if the time cannot be parsed.
  5. //
  6. // Hint: Research sub-string functions for string data type.
  7. // Examples:
  8. // time             result
  9. // 1:00 PM          beer time
  10. // 4:30 PM          beer time
  11. // 10:57 PM         beer time
  12. // 8:30 AM          non-beer time
  13. // 02:59 AM         beer time
  14. // 03:00 AM         non-beer time
  15. // 03:26 AM         non-beer time
  16.  
  17. let input = [ '3:26 AM' ];
  18.  
  19. let print = this.print || console.log;
  20. let gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
  21.  
  22. let userInput = gets();
  23.  
  24. //how to indent that the time for the beer is correct
  25. // constrains - 1:00 PM ~ 3:00 AM
  26.  
  27. // beer time in case is AM check available diapason 1:00 AM to 2:59 AM
  28. // none drinking hours 3:01 AM -> 4:00 AM - 5:00 AM - 6:00 AM - 7:00 AM - 8:00 AM - 9:00 AM - 10:00 AM - 11:00 AM - 12:00 AM - 12:59 AM <-
  29.  
  30. // extract the tt marker from the input
  31. // -- check if the tt marker point PM if so print message
  32.  
  33. let tt = userInput.substring(userInput.length, userInput.length - 2);
  34. let hh = ''; //
  35. let lenghtInput = userInput.length;
  36.  
  37. // check the length of the input
  38. //- in case it is 8 char long slice second char
  39. //- in case it is 7 char long slice first char
  40.  
  41. switch (lenghtInput) {
  42.     case 7:
  43.         hh = Number(userInput.substring(0, 1));
  44.         break;
  45.     case 8:
  46.         hh = Number(userInput.substring(0, 2));
  47.         break;
  48.     default:
  49.         console.log('error');
  50. }
  51.  
  52. if (tt === 'PM') {
  53.     print('beer time');
  54. } else if (tt === 'AM') {
  55.     if (hh > 2) {
  56.         print('non-beer time');
  57.     } else if (hh <= 2) {
  58.         print('beer time');
  59.     }
  60. }
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement