avr39ripe

jsTimeObject

Feb 20th, 2021 (edited)
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Study</title>
  6. </head>
  7. <body>
  8.     <ul id="output">
  9.     </ul>
  10.     <script>
  11.         'use strict'
  12.  
  13.         // format time object to HH:MM:SS by adding '0' where needed and return formated string
  14.         function formatTime(tmObj) {
  15.             let tmStr = {};
  16.             for (let key in tmObj) {
  17.                 tmStr[key] = (tmObj[key] < 9 ? '0' : '' ) + tmObj[key];
  18.             }
  19.             return `${tmStr.hour}:${tmStr.minute}:${tmStr.second}`;
  20.         }
  21.  
  22.         // print formated time object by some printer function.
  23.         // printer function must receive string parameter.
  24.         // By default - print using console.log
  25.         // Returns time object for convenience.
  26.         function printTime(tmObj, printer = (str) => console.log(str)) {
  27.             printer(formatTime(tmObj));
  28.             return tmObj;
  29.         }
  30.  
  31.         // correct time contained in time object to correspond reality :)
  32.         function correctTime(tmObj) {
  33.             const secInMin = 60;
  34.             const minInHour = 60;
  35.             const hourInDay = 24;
  36.  
  37.             if (tmObj.second >= secInMin) {
  38.                 tmObj.minute += Math.floor(tmObj.second / secInMin);
  39.                 tmObj.second %= secInMin;
  40.             }
  41.  
  42.             if (tmObj.minute >= minInHour) {
  43.                 tmObj.hour += Math.floor(tmObj.minute / minInHour);
  44.                 tmObj.minute %= minInHour;
  45.             }
  46.  
  47.             if (tmObj.hour >= hourInDay) {
  48.                 tmObj.hour %= hourInDay;
  49.             }
  50.  
  51.             return tmObj
  52.         }
  53.  
  54.         // add content of second parameter time object to first and return corrected first time object
  55.         function addTime(tmObj, addTmObj) {
  56.             for (let key in tmObj) {
  57.                 tmObj[key] += addTmObj[key];
  58.             }
  59.             return correctTime(tmObj);
  60.         }
  61.  
  62.         function addSeconds(tmObj, seconds) {
  63.             return addTime(tmObj, {hour:0 , minute: 0, second: seconds});
  64.         }
  65.  
  66.         function addMinutes(tmObj, minutes) {
  67.             return addTime(tmObj, { hour: 0, minute: minutes, second: 0});
  68.         }
  69.  
  70.         function addHours(tmObj, hours) {
  71.             return addTime(tmObj, { hour: hours, minute: 0, second: 0 });
  72.         }
  73.  
  74.         // Create new scope, so all variables inside is inaccessible outside {}.
  75.         // We can only pass objects as arguments to our functions.
  76.         // When we call some function, for example printTime(time1);
  77.         // and time1 variable contains reference to some time object, this reference becomes
  78.         // initializer to printTime's tmObj parameter. Looks like if we write tmObj = time1 somehow.
  79.         // This way reference to time object is copied to formal parameter tmObj so printTime
  80.         // can access time object. There is no need to have DIRECT access to time1 variable
  81.         // inside printTime function.
  82.         {
  83.             // printer implementation for HTML output
  84.             let printerHTML = function (str) {
  85.                 output.innerHTML += `<li>${str}</li>`
  86.             }
  87.  
  88.             // printer implementation for alert output
  89.             let printerAlert = function (str) {
  90.                 alert(str);
  91.             }
  92.  
  93.             let printer; // Leave printer uninitializet,so undefined for default console.log output
  94.             // or uncomment on of next line
  95.             printer = printerHTML;
  96.             //printer = printerAlert;
  97.  
  98.             let time1 = { hour: 22, minute: 33, second: 44 };
  99.             let time2 = { hour: 1, minute: 2, second: 3 };
  100.             let time3 = { hour: 12, minute: 26, second: 7 };
  101.  
  102.             printTime(time1, printer);
  103.             printTime(time2, printer);
  104.             printTime(time3, printer);
  105.  
  106.             printTime(correctTime({ hour: 12, minute: 67, second: 23 }), printer);
  107.             printTime(correctTime({ hour: 23, minute: 59, second: 61 }), printer);
  108.  
  109.             printTime(
  110.                 addHours(printTime(correctTime({ hour: 23, minute: 59, second: 61 }), printer), 2)
  111.                 , printer
  112.             );
  113.  
  114.             printTime(addMinutes({ hour: 23, minute: 59, second: 61 }, 26), printer);
  115.  
  116.             printTime(addHours({ hour: 23, minute: 59, second: 61 }, 7),printer);
  117.         }
  118.  
  119.     </script>
  120. </body>
  121. </html>
Add Comment
Please, Sign In to add comment