Advertisement
Lulunga

DOM 02. Number Convertor

Oct 21st, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     // create TO options: : Binary and Hexadecimal
  3.     // create functions to convert decimal to binary and decimal to hexadecimal.
  4.     //  When the [Convert it] button is clicked, the expected result should appear in the [Result] input field.
  5.  
  6.     const selectMenuTo = document.getElementById('selectMenuTo');
  7.     document
  8.     .querySelector("#container > button")
  9.     .addEventListener('click', convert);
  10.     function convert() {
  11.         debugger;
  12.         let number = Number(document.getElementById("input").value);
  13.         let result;
  14.         if (selectMenuTo.value === 'binary') {
  15.             result = decimalToBinary(number);
  16.         } else if (selectMenuTo.value === 'hexadecimal') {
  17.             result = decimalToHexadecimal(number);
  18.         }
  19.         appendResult(result);
  20.     }
  21.     function appendResult(result) {
  22.         document.getElementById('result').value = result;
  23.     }
  24.     function decimalToBinary(number) {
  25.         return (number >>> 0).toString(2);
  26.     }
  27.     function decimalToHexadecimal(number) {
  28.         return number.toString(16).toUpperCase();
  29.     }
  30.  
  31.     function createSelectMenuOptions() {
  32.         let binaryOption = document.createElement('option');
  33.         binaryOption.textContent = 'Binary';
  34.         binaryOption.value = 'binary';
  35.         let hexadecimalOption = document.createElement('option');
  36.         hexadecimalOption.textContent = 'Hexadecimal';
  37.         hexadecimalOption.value = 'hexadecimal';
  38.  
  39.         selectMenuTo.appendChild(binaryOption);
  40.         selectMenuTo.appendChild(hexadecimalOption);
  41.     };
  42.     createSelectMenuOptions();
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement