Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // A beer time is after 1:00 PM and before 3:00 AM.
- // Write a program that enters a time in format “hh:mm tt” (an hour in range [01...12],
- // a minute in range [00…59] and AM / PM designator) and prints beer time or non-beer time according to
- // the definition above or invalid time if the time cannot be parsed.
- //
- // Hint: Research sub-string functions for string data type.
- // Examples:
- // time result
- // 1:00 PM beer time
- // 4:30 PM beer time
- // 10:57 PM beer time
- // 8:30 AM non-beer time
- // 02:59 AM beer time
- // 03:00 AM non-beer time
- // 03:26 AM non-beer time
- let input = [ '3:26 AM' ];
- let print = this.print || console.log;
- let gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
- let userInput = gets();
- //how to indent that the time for the beer is correct
- // constrains - 1:00 PM ~ 3:00 AM
- // beer time in case is AM check available diapason 1:00 AM to 2:59 AM
- // 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 <-
- // extract the tt marker from the input
- // -- check if the tt marker point PM if so print message
- let tt = userInput.substring(userInput.length, userInput.length - 2);
- let hh = ''; //
- let lenghtInput = userInput.length;
- // check the length of the input
- //- in case it is 8 char long slice second char
- //- in case it is 7 char long slice first char
- switch (lenghtInput) {
- case 7:
- hh = Number(userInput.substring(0, 1));
- break;
- case 8:
- hh = Number(userInput.substring(0, 2));
- break;
- default:
- console.log('error');
- }
- if (tt === 'PM') {
- print('beer time');
- } else if (tt === 'AM') {
- if (hh > 2) {
- print('non-beer time');
- } else if (hh <= 2) {
- print('beer time');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement