-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
104 lines (95 loc) · 3.49 KB
/
script.js
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const fromSelected = document.getElementById("from-select");
const toSelected = document.getElementById("to-select");
const from = document.getElementById("from-input");
const to = document.getElementById("to-input");
const error = document.getElementById("error");
let fromNS = "Binary"
let toNS = "Binary";
fromSelected.addEventListener("change", function () {
fromNS = fromSelected.options[fromSelected.selectedIndex].text;
from.placeholder = fromNS + " Number";
});
toSelected.addEventListener("change", function () {
toNS = toSelected.options[toSelected.selectedIndex].text;
to.placeholder = toNS + " Number";
});
from.addEventListener("input", function () {
error.style.display = "none";
});
let fromValue;
document.getElementById("convert-button").addEventListener("click", function () {
switch (fromNS) {
case "Binary":
fromValue = from.value;
if (/^[01]*$/.test(fromValue)) {
switch (toNS) {
case "Decimal": to.value = parseInt(fromValue, 2);
break;
case "Hexadecimal": to.value = parseInt(fromValue, 2).toString(16).toUpperCase();
break;
case "Octal": to.value = parseInt(fromValue, 2).toString(8);
break;
default: to.value = fromValue;
}
} else {
error.style.display = "inherit";
error.innerText = "Invalid " + fromNS + " Number";
to.value = "";
}
break;
case "Decimal":
fromValue = from.value;
if (/^[0-9]*$/.test(fromValue)) {
switch (toNS) {
case "Binary": to.value = parseInt(fromValue, 10).toString(2);
break;
case "Hexadecimal": to.value = parseInt(fromValue, 10).toString(16).toUpperCase();
break;
case "Octal": to.value = parseInt(fromValue, 10).toString(8);
break;
default: to.value = fromValue;
}
} else {
error.style.display = "inherit";
error.innerText = "Invalid " + fromNS + " Number";
to.value = "";
}
break;
case "Hexadecimal":
fromValue = from.value;
if (/^[0-9a-fA-F]*$/.test(fromValue)) {
switch (toNS) {
case "Binary": to.value = parseInt(fromValue, 16).toString(2);
break;
case "Decimal": to.value = parseInt(fromValue, 16);
break;
case "Octal": to.value = parseInt(fromValue, 16).toString(8);
break;
default: to.value = fromValue;
}
} else {
error.style.display = "inherit";
error.innerText = "Invalid " + fromNS + " Number";
to.value = "";
}
break;
case "Octal":
fromValue = from.value;
if (/^[0-7]*$/.test(fromValue)) {
switch (toNS) {
case "Binary": to.value = parseInt(fromValue, 8).toString(2);
break;
case "Decimal": to.value = parseInt(fromValue, 8);
break;
case "Hexadecimal": to.value = parseInt(fromValue, 8).toString(16).toUpperCase();
break;
default: to.value = fromValue;
}
} else {
error.style.display = "inherit";
error.innerText = "Invalid " + fromNS + " Number";
to.value = "";
}
break;
}
});