Spocoman

08. On Time for the Exam

Dec 20th, 2021 (edited)
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function onTimeForTheExam(input) {
  2.     let hourExam = parseInt(input[0]);
  3.     let minuteExam = parseInt(input[1]);
  4.     let hoursStudent = parseInt(input[2]);
  5.     let minuteStudent = parseInt(input[3]);
  6.  
  7.     let examMinutes = hourExam * 60 + minuteExam;
  8.     let studentMinutes = hoursStudent * 60 + minuteStudent;
  9.     let time = Math.abs(studentMinutes - examMinutes);
  10.  
  11.     if (studentMinutes > examMinutes) {
  12.         console.log("Late");
  13.         if (time < 60) {
  14.             console.log(`${time} minutes after the start`);
  15.         } else if (time % 60 < 10) {
  16.             console.log(`${parseInt(time / 60)}:0${time % 60} hours after the start`);
  17.         } else {
  18.             console.log(`${parseInt(time / 60)}:${time % 60} hours after the start`);
  19.         }
  20.     } else {
  21.         if (time <= 30) {
  22.             console.log("On time");
  23.         } else {
  24.             console.log("Early");
  25.         }
  26.         if (time !== 0) {
  27.             if (time < 60) {
  28.                 console.log(`${time} minutes before the start`);
  29.             } else if (time >= 60 && time % 60 < 10) {
  30.                 console.log(`${parseInt(time / 60)}:0${time % 60} hours before the start`);
  31.             } else if (time !== 0) {
  32.                 console.log(`${parseInt(time / 60)}:${time % 60} hours before the start`);
  33.             }
  34.         }
  35.     }
  36. }
  37.  
  38. РЕШЕНИЕ СЪС String() И PadStart():
  39.  
  40. function onTimeForTheExam(input) {
  41.     let hourExam = parseInt(input[0]);
  42.     let minuteExam = parseInt(input[1]);
  43.     let hoursStudent = parseInt(input[2]);
  44.     let minuteStudent = parseInt(input[3]);
  45.  
  46.     let examMinutes = hourExam * 60 + minuteExam;
  47.     let studentMinutes = hoursStudent * 60 + minuteStudent;
  48.     let time = Math.abs(studentMinutes - examMinutes);
  49.  
  50.     if (studentMinutes > examMinutes) {
  51.         console.log("Late");
  52.         if (time < 60) {
  53.             console.log(`${time} minutes after the start`);
  54.         } else {
  55.             console.log(`${parseInt(time / 60)}:${String(time % 60).padStart(2, '0')} hours after the start`);
  56.         }
  57.     } else {
  58.         if (time <= 30) {
  59.             console.log("On time");
  60.         } else {
  61.             console.log("Early");
  62.         }
  63.         if (time !== 0) {
  64.             if (time < 60) {
  65.                 console.log(`${time} minutes before the start`);
  66.             } else {
  67.                 console.log(`${parseInt(time / 60)}:${String(time % 60).padStart(2, '0')} hours before the start`);
  68.             }
  69.         }
  70.     }
  71. }
  72.  
  73. РЕШЕНИЕ С ТЕРНАРЕН ОПЕРАТОР:
  74.  
  75. function onTimeForTheExam(input) {
  76.     let hourExam = parseInt(input[0]);
  77.     let minuteExam = parseInt(input[1]);
  78.     let hoursStudent = parseInt(input[2]);
  79.     let minuteStudent = parseInt(input[3]);
  80.  
  81.     let examMinutes = hourExam * 60 + minuteExam;
  82.     let studentMinutes = hoursStudent * 60 + minuteStudent;
  83.     let time = Math.abs(studentMinutes - examMinutes);
  84.  
  85.     console.log(studentMinutes > examMinutes ? "Late" : time <= 30 ? "On time" : "Early");
  86.     console.log(time !== 0 ? (time < 60 ? `${time} minutes ${studentMinutes > examMinutes ? 'after' : 'before'} the start` :
  87.         `${parseInt(time / 60)}:${time % 60 < 10 ? '0' : ''}${time % 60} hours ${studentMinutes > examMinutes ? 'after' : 'before'} the start`) : '');
  88. }
  89.  
  90. И ЛЕКО ТАРИКАТСКАТА:)
  91.  
  92. function onTimeForTheExam(input) {
  93.     let examMinutes = parseInt(input[0]) * 60 + parseInt(input[1]);
  94.     let studentMinutes = parseInt(input[2]) * 60 + parseInt(input[3]);
  95.     let time = Math.abs(studentMinutes - examMinutes);
  96.  
  97.     console.log(studentMinutes > examMinutes ? "Late" : time <= 30 ? "On time" : "Early");
  98.     console.log(time !== 0 ? (time < 60 ? `${time} minutes ${studentMinutes > examMinutes ? 'after' : 'before'} the start` :
  99.         `${parseInt(time / 60)}:${time % 60 < 10 ? '0' : ''}${time % 60} hours ${studentMinutes > examMinutes ? 'after' : 'before'} the start`) : '');
  100. }
  101.  
Add Comment
Please, Sign In to add comment