-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
54 lines (54 loc) · 2.05 KB
/
script.js
File metadata and controls
54 lines (54 loc) · 2.05 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
window.onload = function() {
var memory = 0;
var display = document.getElementById('display');
var buttons = document.querySelectorAll('input[type="button"]');
buttons.forEach(function(button) {
button.addEventListener('click', function() {
switch(this.value) {
case '=':
try {
display.value = new Function('return ' + display.value)();
} catch(e) {
display.value = 'Error';
}
break;
case 'AC':
display.value = '';
break;
case 'DE':
display.value = display.value.slice(0, -1);
break;
case 'M+':
memory += parseFloat(display.value) || 0;
break;
case 'MR':
display.value = memory.toString();
break;
case 'sin':
case 'cos':
case 'tan':
try {
display.value = Math[this.value](parseFloat(display.value) * Math.PI / 180).toString();
} catch (e) {
display.value = 'Error';
}
break;
case 'log':
case 'ln':
try {
if (parseFloat(display.value) > 0) {
display.value = this.value === 'log' ? Math.log10(parseFloat(display.value)).toString() :
Math.log(parseFloat(display.value)).toString();
} else {
display.value = 'Error';
}
} catch (e) {
display.value = 'Error';
}
break;
default:
display.value += this.value;
}
});
});
}