-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
86 lines (79 loc) · 2.03 KB
/
app.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
const input = document.querySelector('.input');
const output = document.querySelector('.output');
const dellete = document.querySelector('.AC');
dellete.addEventListener('click', () => {
input.innerHTML = '';
output.innerHTML = '';
});
document.querySelectorAll('.number').forEach((item) => {
item.addEventListener('click', () => {
input.innerHTML += item.innerHTML;
});
});
let counter = 0;
//preventing the user to addmore than one point
const dote = document.querySelector('.point');
dote.addEventListener('click', () => {
if (counter === 0) {
input.innerHTML += dote.innerHTML;
} else {
return;
}
counter++;
console.log(counter);
});
let state;
document.querySelectorAll('.operator').forEach((item) => {
item.addEventListener('click', () => {
counter = 0;
output.innerHTML = input.innerHTML + ' ' + item.innerHTML;
input.innerText = '';
switch (item.innerHTML) {
case '+':
state = '+';
break;
case '-':
state = '-';
break;
case '/':
state = '/';
break;
case '%':
state = '%';
break;
case '*':
state = '*';
}
console.log(state);
});
});
document.querySelector('.equal').addEventListener('click', () => {
if (input.innerHTML === '') {
return;
} else {
output.innerHTML += ' ' + input.innerHTML;
switch (state) {
case '+':
input.innerHTML =
parseFloat(input.innerHTML) + parseFloat(output.innerHTML);
break;
case '-':
input.innerHTML =
parseFloat(output.innerHTML) - parseFloat(input.innerHTML);
break;
case '/':
input.innerHTML =
parseFloat(output.innerHTML) / parseFloat(input.innerHTML);
break;
case '%':
input.innerHTML =
(parseFloat(output.innerHTML) * 100) / parseFloat(input.innerHTML) +
'%';
case '*':
input.innerHTML =
parseFloat(output.innerHTML) * parseFloat(input.innerHTML);
break;
}
}
state = undefined;
});