-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
80 lines (65 loc) · 2.97 KB
/
Copy pathscript.js
File metadata and controls
80 lines (65 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
function convert() {
var inputType = document.getElementById("inputType").value;
var inputNumber = document.getElementById("inputNumber").value;
var outputDiv = document.getElementById("output");
var output = "";
if (inputNumber === "") {
alert("Please enter a number.");
return;
}
if (inputType === "decimal") {
output += "<div class='result'>Binary representation: " + decimal_to_binary(inputNumber) + "</div>";
output += "<div class='result'>Hexadecimal representation: " + decimal_to_hexadecimal(inputNumber) + "</div>";
output += "<div class='result'>Octal representation: " + decimal_to_octal(inputNumber) + "</div>";
} else if (inputType === "binary") {
output += "<div class='result'>Decimal representation: " + binary_to_decimal(inputNumber) + "</div>";
output += "<div class='result'>Hexadecimal representation: " + binary_to_hexadecimal(inputNumber) + "</div>";
output += "<div class='result'>Octal representation: " + binary_to_octal(inputNumber) + "</div>";
} else if (inputType === "hexadecimal") {
output += "<div class='result'>Decimal representation: " + hexadecimal_to_decimal(inputNumber) + "</div>";
output += "<div class='result'>Binary representation: " + hexadecimal_to_binary(inputNumber) + "</div>";
output += "<div class='result'>Octal representation: " + hexadecimal_to_octal(inputNumber) + "</div>";
} else if (inputType === "octal") {
output += "<div class='result'>Decimal representation: " + octal_to_decimal(inputNumber) + "</div>";
output += "<div class='result'>Binary representation: " + octal_to_binary(inputNumber) + "</div>";
output += "<div class='result'>Hexadecimal representation: " + octal_to_hexadecimal(inputNumber) + "</div>";
}
outputDiv.innerHTML = output;
outputDiv.style.display = "block";
}
function decimal_to_binary(decimal) {
return parseInt(decimal).toString(2);
}
function decimal_to_hexadecimal(decimal) {
return parseInt(decimal).toString(16);
}
function decimal_to_octal(decimal) {
return parseInt(decimal).toString(8);
}
function binary_to_decimal(binary) {
return parseInt(binary, 2).toString();
}
function binary_to_hexadecimal(binary) {
return parseInt(binary, 2).toString(16);
}
function binary_to_octal(binary) {
return parseInt(binary, 2).toString(8);
}
function hexadecimal_to_decimal(hexadecimal) {
return parseInt(hexadecimal, 16).toString();
}
function hexadecimal_to_binary(hexadecimal) {
return parseInt(hexadecimal, 16).toString(2);
}
function hexadecimal_to_octal(hexadecimal) {
return parseInt(hexadecimal, 16).toString(8);
}
function octal_to_decimal(octal) {
return parseInt(octal, 8).toString();
}
function octal_to_binary(octal) {
return parseInt(octal, 8).toString(2);
}
function octal_to_hexadecimal(octal) {
return parseInt(octal, 8).toString(16);
}