forked from swapnilsparsh/30DaysOfJavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
24 lines (21 loc) · 666 Bytes
/
Copy pathscript.js
File metadata and controls
24 lines (21 loc) · 666 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function convert() {
let to_convert = document.getElementById("to_convert").value;
to_convert = parseFloat(to_convert);
// Function to convert decimal to binary
function decimalToBinary(number) {
if (number === 0) {
return "0";
}
let binary = "";
while (number > 0) {
binary = (number % 2).toString() + binary;
number = Math.floor(number / 2);
}
return binary;
}
// Convert the number to binary
let binaryResult = decimalToBinary(to_convert);
// Visualizing results
let final = document.getElementById("res");
final.value = binaryResult;
}