SHARE
TWEET

Untitled

a guest Jan 24th, 2019 124 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     //add select options
  3.     let selectElement = document.querySelector('#selectMenuTo');
  4.     let binaryOption = document.createElement('option');
  5.     let hexadecimalOption = document.createElement('option');
  6.     binaryOption.value = 'binary';
  7.     binaryOption.text = 'Binary';
  8.     hexadecimalOption.value = 'hexadecimal';
  9.     hexadecimalOption.text = 'Hexadecimal';
  10.     selectElement.add(binaryOption);
  11.     selectElement.add(hexadecimalOption);
  12.  
  13.     //add click event
  14.     document.querySelector('button').addEventListener('click', clickEvent)
  15.  
  16.     function clickEvent() {
  17.         //get values
  18.         let decimalNumber = +document.querySelector('#input').value;
  19.         let selectElement = document.querySelector('#selectMenuTo');
  20.         let convertTo = selectElement.options[selectElement.selectedIndex].text;
  21.  
  22.         //convert
  23.         let result;
  24.         switch (convertTo) {
  25.             case 'Binary':
  26.                 result = decimalNumber.toString(2);
  27.                 break;
  28.             case 'Hexadecimal':
  29.                 result = decimalNumber.toString(16).toUpperCase();
  30.                 break;
  31.         }
  32.  
  33.         //append output
  34.         document.querySelector('#result').value = result;
  35.     }
  36. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top