-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
36 lines (35 loc) · 1 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
function onButton(e) {
var btn = e.target || e.srcElement;
var action = document.getElementById(btn.id).innerHTML;
var res = document.getElementById('res');
switch(action) {
case '0':
case '1':
case '+':
case '-':
case '*':
case '/':
res.innerHTML += action;
break;
case 'C':
res.innerHTML = ' ';
break;
case '=':
var expr = res.innerHTML;
var nums = /(\d+)/g;
// Replace all base 2 nums with base 10 equivs
expr = expr.replace(nums, function(match) {
return parseInt(match, 2);
})
// eval in base 10 and convert to base 2
res.innerHTML = eval(expr).toString(2);
break;
default:
console.error('should not be executed');
break;
}
}
var buttons = document.getElementsByTagName('button');
for (let button of buttons) {
button.onclick = onButton;
}