-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
79 lines (73 loc) · 1.95 KB
/
main.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
let screenOutput = document.querySelector('.screen');
let storeVal = 0;
let valueLength = 0;
let expression = 0;
function init(){
document.querySelector('.calc-buttons').addEventListener('click', function(event) {
buttonClick(event.target.innerText);
});
}
function buttonClick(value) {
if (isNaN(parseInt(value))) {
switch (value) {
case 'C':
storeVal = 0;
screenOutput.innerText = 0;
break;
case '←':
if (screenOutput.innerText.length !== 1) {
valueLength = screenOutput.innerText.length;
screenOutput.innerText = screenOutput.innerText.substr(0, valueLength - 1);
break;
}
screenOutput.innerText = 0;
break;
case '=':
expression = new String(storeVal + screenOutput.innerText);
console.log(expression);
expression = eval(expression.substr(1));
storeVal = 0;
console.log(storeVal);
screenOutput.innerText = (expression);
break;
case '+':
storeVal += screenOutput.innerText;
console.log(storeVal);
storeVal += '+';
console.log(storeVal);
screenOutput.innerText = 0;
console.log(storeVal);
break;
case '-':
storeVal += screenOutput.innerText;
storeVal += '-';
screenOutput.innerText = 0;
break;
case '×':
storeVal += screenOutput.innerText;
console.log(storeVal);
storeVal += '*';
console.log(storeVal);
screenOutput.innerText = 0;
console.log(storeVal);
break;
case '÷':
storeVal += screenOutput.innerText;
storeVal += '/';
screenOutput.innerText = 0;
break;
default:
break;
}
} else {
inputNumber(value);
}
}
function inputNumber(number) {
if ((screenOutput.innerText.substr(0,1) === '0')) {
screenOutput.innerText = number;
} else {
screenOutput.innerText += number;
}
}
init();