-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
58 lines (52 loc) · 2.33 KB
/
Copy pathscript.js
File metadata and controls
58 lines (52 loc) · 2.33 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
// parseFloat(num, base) used for convert number any to decimal
// num.toString(base) used for convert number decimal to any
// toUpperCase used for capitalized the converted values. Also convert my Hex when I type and when I receive the result
// It will be more interesting , If i add regex in switch
let d = document.getElementById("d"),
b = document.getElementById("b"),
o = document.getElementById("o"),
h = document.getElementById("h")
m = document.getElementById("m")
l = document.getElementById("l")
function calculate(val, base, which) {
if (val.length === 0) {
d.value = null,
b.value = null,
o.value = null,
h.value = null
m.value = null
l.value = null
return
}
let inputtedVal = parseFloat(val, base) // decimal value
// results
switch(which) {
case 'b':
d.value = inputtedVal.toString(10).toUpperCase()
o.value = inputtedVal.toString(8).toUpperCase()
h.value = inputtedVal.toString(16).toUpperCase()
m.value = Math.floor((b.value / Math.pow(10, b.value.toString().length - 1 )) % 10)
l.value = b.value % 10
break
case 'o':
d.value = inputtedVal.toString(10).toUpperCase()
b.value = inputtedVal.toString(2).toUpperCase()
h.value = inputtedVal.toString(16).toUpperCase()
m.value = Math.floor((b.value / Math.pow(10, b.value.toString().length - 1 )) % 10)
l.value = b.value % 10
break
case 'h':
d.value = inputtedVal.toString(10).toUpperCase()
b.value = inputtedVal.toString(2).toUpperCase()
o.value = inputtedVal.toString(8).toUpperCase()
m.value = inputtedVal.toString(8).toUpperCase()
l.value = inputtedVal.toString(8).toUpperCase()
break
default:
b.value = inputtedVal.toString(2).toUpperCase()
o.value = inputtedVal.toString(8).toUpperCase()
h.value = inputtedVal.toString(16).toUpperCase()
m.value = Math.floor((b.value / Math.pow(10, b.value.toString().length - 1 )) % 10)
l.value = b.value % 10
}
}