Advertisement
kstoyanov

07. Number Convertor

Sep 29th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.  
  3.     const menu = document.getElementById('selectMenuTo');
  4.     const optionBinary = document.createElement('option');
  5.     optionBinary.value = 'binary';
  6.     optionBinary.text = 'Binary';
  7.     menu.appendChild(optionBinary);
  8.  
  9.     const optionHexadecimal = document.createElement('option');
  10.     optionHexadecimal.value = 'hexadecimal';
  11.     optionHexadecimal.text = 'Hexadecimal';
  12.     menu.appendChild(optionHexadecimal);
  13.  
  14.     document.querySelector('button').addEventListener('click', handleClick);
  15.  
  16.     function handleClick() {
  17.         let number = document.getElementById('input').value;
  18.         let result = convertNumber(number);
  19.         showResult(result);
  20.     }
  21.  
  22.     function convertNumber(number) {
  23.         let result;
  24.         if (menu.value === 'binary') {
  25.             result = Number(number).toString(2);
  26.         } else if (menu.value === 'hexadecimal') {
  27.             result = Number(number).toString(16).toUpperCase();
  28.         }
  29.         return result;
  30.     }
  31.  
  32.     function showResult(result) {
  33.         const outputField = document.getElementById('result')
  34.         outputField.value = result;
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement