Advertisement
kstoyanov

03. Time Converter

Oct 1st, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function attachEventsListeners() {
  2.     Array.from(
  3.         document.querySelectorAll('div input[type=button]')
  4.     ).forEach((button) => button.addEventListener('click', buttonClickHandler));
  5.  
  6.     function buttonClickHandler(e) {
  7.         const inputValue = e.target.parentNode.children[1].value;
  8.         const currentId = e.target.id;
  9.  
  10.         if (!inputValue) return;
  11.  
  12.         const funcObj = {
  13.             daysBtn: (val) =>
  14.                 setTimeValues(val, val * 24, val * 1440, val * 86400),
  15.             hoursBtn: (val) =>
  16.                 setTimeValues(val / 24, val, val * 60, val * 3600),
  17.             minutesBtn: (val) =>
  18.                 setTimeValues(val / 1440, val / 60, val, val * 60),
  19.             secondsBtn: (val) =>
  20.                 setTimeValues(val / 86400, val / 3600, val / 60, val),
  21.         };
  22.         funcObj[currentId](inputValue);
  23.     }
  24.  
  25.     function setTimeValues(days, hours, minutes, seconds) {
  26.         document.getElementById('days').value = days;
  27.         document.getElementById('hours').value = hours;
  28.         document.getElementById('minutes').value = minutes;
  29.         document.getElementById('seconds').value = seconds;
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement