Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve() {
- // create TO options: : Binary and Hexadecimal
- // create functions to convert decimal to binary and decimal to hexadecimal.
- // When the [Convert it] button is clicked, the expected result should appear in the [Result] input field.
- const selectMenuTo = document.getElementById('selectMenuTo');
- document
- .querySelector("#container > button")
- .addEventListener('click', convert);
- function convert() {
- debugger;
- let number = Number(document.getElementById("input").value);
- let result;
- if (selectMenuTo.value === 'binary') {
- result = decimalToBinary(number);
- } else if (selectMenuTo.value === 'hexadecimal') {
- result = decimalToHexadecimal(number);
- }
- appendResult(result);
- }
- function appendResult(result) {
- document.getElementById('result').value = result;
- }
- function decimalToBinary(number) {
- return (number >>> 0).toString(2);
- }
- function decimalToHexadecimal(number) {
- return number.toString(16).toUpperCase();
- }
- function createSelectMenuOptions() {
- let binaryOption = document.createElement('option');
- binaryOption.textContent = 'Binary';
- binaryOption.value = 'binary';
- let hexadecimalOption = document.createElement('option');
- hexadecimalOption.textContent = 'Hexadecimal';
- hexadecimalOption.value = 'hexadecimal';
- selectMenuTo.appendChild(binaryOption);
- selectMenuTo.appendChild(hexadecimalOption);
- };
- createSelectMenuOptions();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement