Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Study</title>
- </head>
- <body>
- <ul id="output">
- </ul>
- <script>
- 'use strict'
- // format time object to HH:MM:SS by adding '0' where needed and return formated string
- function formatTime(tmObj) {
- let tmStr = {};
- for (let key in tmObj) {
- tmStr[key] = (tmObj[key] < 9 ? '0' : '' ) + tmObj[key];
- }
- return `${tmStr.hour}:${tmStr.minute}:${tmStr.second}`;
- }
- // print formated time object by some printer function.
- // printer function must receive string parameter.
- // By default - print using console.log
- // Returns time object for convenience.
- function printTime(tmObj, printer = (str) => console.log(str)) {
- printer(formatTime(tmObj));
- return tmObj;
- }
- // correct time contained in time object to correspond reality :)
- function correctTime(tmObj) {
- const secInMin = 60;
- const minInHour = 60;
- const hourInDay = 24;
- if (tmObj.second >= secInMin) {
- tmObj.minute += Math.floor(tmObj.second / secInMin);
- tmObj.second %= secInMin;
- }
- if (tmObj.minute >= minInHour) {
- tmObj.hour += Math.floor(tmObj.minute / minInHour);
- tmObj.minute %= minInHour;
- }
- if (tmObj.hour >= hourInDay) {
- tmObj.hour %= hourInDay;
- }
- return tmObj
- }
- // add content of second parameter time object to first and return corrected first time object
- function addTime(tmObj, addTmObj) {
- for (let key in tmObj) {
- tmObj[key] += addTmObj[key];
- }
- return correctTime(tmObj);
- }
- function addSeconds(tmObj, seconds) {
- return addTime(tmObj, {hour:0 , minute: 0, second: seconds});
- }
- function addMinutes(tmObj, minutes) {
- return addTime(tmObj, { hour: 0, minute: minutes, second: 0});
- }
- function addHours(tmObj, hours) {
- return addTime(tmObj, { hour: hours, minute: 0, second: 0 });
- }
- // Create new scope, so all variables inside is inaccessible outside {}.
- // We can only pass objects as arguments to our functions.
- // When we call some function, for example printTime(time1);
- // and time1 variable contains reference to some time object, this reference becomes
- // initializer to printTime's tmObj parameter. Looks like if we write tmObj = time1 somehow.
- // This way reference to time object is copied to formal parameter tmObj so printTime
- // can access time object. There is no need to have DIRECT access to time1 variable
- // inside printTime function.
- {
- // printer implementation for HTML output
- let printerHTML = function (str) {
- output.innerHTML += `<li>${str}</li>`
- }
- // printer implementation for alert output
- let printerAlert = function (str) {
- alert(str);
- }
- let printer; // Leave printer uninitializet,so undefined for default console.log output
- // or uncomment on of next line
- printer = printerHTML;
- //printer = printerAlert;
- let time1 = { hour: 22, minute: 33, second: 44 };
- let time2 = { hour: 1, minute: 2, second: 3 };
- let time3 = { hour: 12, minute: 26, second: 7 };
- printTime(time1, printer);
- printTime(time2, printer);
- printTime(time3, printer);
- printTime(correctTime({ hour: 12, minute: 67, second: 23 }), printer);
- printTime(correctTime({ hour: 23, minute: 59, second: 61 }), printer);
- printTime(
- addHours(printTime(correctTime({ hour: 23, minute: 59, second: 61 }), printer), 2)
- , printer
- );
- printTime(addMinutes({ hour: 23, minute: 59, second: 61 }, 26), printer);
- printTime(addHours({ hour: 23, minute: 59, second: 61 }, 7),printer);
- }
- </script>
- </body>
- </html>
Add Comment
Please, Sign In to add comment