Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function attachEventsListeners() {
  2.     let buttons = document.querySelectorAll('div input[type="button"]');
  3.  
  4.     for (let button of buttons) {
  5.         button.addEventListener('click', convertUnits)
  6.     }
  7.  
  8.     function convertUnits() {
  9.         let time = Number(this.parentNode.children[1].value);
  10.         time = isNaN(time) ? 0 : time;
  11.         let unit = this.parentNode.children[1].id;
  12.  
  13.         // time to seconds
  14.         //noinspection FallThroughInSwitchStatementJS
  15.         switch (unit){
  16.             case 'days':
  17.                 time *= 24;
  18.             case 'hours':
  19.                 time *= 60;
  20.             case 'minutes':
  21.                 time *= 60;
  22.         }
  23.  
  24.         document.getElementById('seconds').value = time;
  25.         time /= 60;
  26.         document.getElementById('minutes').value = time;
  27.         time /= 60;
  28.         document.getElementById('hours').value = time;
  29.         time /= 24;
  30.         document.getElementById('days').value = time;
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement