Skip to content

Commit

Permalink
Add: Decimal to Binary - add the Decimal to binary function which ret…
Browse files Browse the repository at this point in the history
…urn the binary form of decimal value
  • Loading branch information
dineshsutihar committed Apr 6, 2024
1 parent 5bfe7a0 commit 04f0e09
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions day-31-DecimaltoBinaryConverter/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const numberInput = document.getElementById("number-input");
const convertBtn = document.getElementById("convert-btn");
const result = document.getElementById("result");

const decimalToBinary = (input) => {
if (input === 0 || input === 1) {
return String(input);
} else {
return decimalToBinary(Math.floor(input / 2)) + (input % 2);
}
};

function showAnimation(){

}

const checkUserInput = () => {
if (!numberInput.value || isNaN(parseInt(numberInput.value))) {
alert("Please provide a decimal number");
return;
}

result.textContent = decimalToBinary(parseInt(numberInput.value));
numberInput.value = "";
};

convertBtn.addEventListener("click", checkUserInput);

numberInput.addEventListener("keydown", (e) => {
if (e.key === "Enter") {
checkUserInput();
}
});

0 comments on commit 04f0e09

Please sign in to comment.