Advertisement
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>
- <div id="output"></div>
- <script>
- 'use strict'
- function makeTime(h=0, m=0, s=0) {
- let tmObj = { hour: h, minute: m, second: s };
- let tmFunctions = {
- format: function () {
- let tmStr = {};
- for (let key in tmObj) {
- tmStr[key] = (tmObj[key] < 9 ? '0' : '') + tmObj[key];
- }
- return `${tmStr.hour}:${tmStr.minute}:${tmStr.second}`;
- },
- print: function (printer = (str) => console.log(str)) {
- printer(tmFunctions.format(tmObj));
- return tmFunctions;
- },
- correct: function () {
- 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 tmFunctions;
- },
- add: function (addTmObj) {
- for (let key in tmObj) {
- tmObj[key] += addTmObj[key];
- }
- return tmFunctions.correct();
- },
- addSeconds: function (seconds) {
- return tmFunctions.add({ hour: 0, minute: 0, second: seconds });
- },
- addMinutes: function (minutes) {
- return tmFunctions.add({ hour: 0, minute: minutes, second: 0 });
- },
- addHours: function (hours) {
- return tmFunctions.add({ hour: hours, minute: 0, second: 0 });
- }
- };
- return tmFunctions;
- }
- //function makePoint(pX = 0, pY = 0) {
- // let x = pX;
- // let y = pY;
- // let funObj = {
- // setX(pX) { x = pX; },
- // setY(pY) { y = pY; },
- // getX() { return x; },
- // getY() { return y; },
- // print() { console.log(`(${x},${y})`); }
- // };
- // return funObj;
- //}
- function makePoint(pX = 0, pY = 0) {
- let pointObj = { x: pX, y: pY };
- let funObj = {
- setX(pX) {
- if (pX >= 0) { pointObj.x = pX; return true }; return false; },
- setY(pY) {
- if (pY >= 0) { pointObj.y = pY; return true; }; return false; },
- getX() { return pointObj.x; },
- getY() { return pointObj.y; },
- print() { console.log(`(${pointObj.x},${pointObj.y})`); }
- };
- return funObj;
- }
- {
- //let p1 = makePoint();
- //p1.print();
- //p1.setX(-10);
- //p1.setY(20);
- //p1.print();
- let time1 = makeTime(9, 48, 26);
- let time2 = makeTime(11, 26, 7);
- time1.print();
- time1.add({ hour: 12, minute: 67, second: 23 }).print();
- time1.addMinutes(67).print();
- time2.print().addHours(20).print();
- //// 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>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement