-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
37 lines (36 loc) · 1.34 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
37
// Necessary HTML Tags or Buttons
const preview = document.getElementById('preview');
const result = document.getElementById('result');
const buttons = document.querySelectorAll('button');
// All Functions
for (let button of buttons) {
button.addEventListener('click', function (e) {
let myInput = e.target.innerText;
if (myInput == 'AC') {
preview.innerText = '';
result.innerText = '';
} else if (myInput == '⌫') {
const previousNumber = preview.innerText;
preview.innerText = previousNumber.substring(0, previousNumber.length - 1);
result.innerText = '';
} else if (myInput == '%') {
result.innerText = Number(preview.innerText) / 100;
preview.innerText = Number(result.innerText);
} else if (myInput == '×') {
myInput = '*';
preview.innerText += '*';
} else if (myInput == '÷') {
myInput = '/';
preview.innerText += '/';
} else if (myInput == '=') {
const evalConvert = eval(preview.innerText);
if (typeof(evalConvert) != 'number') {
alert('Something is happening wrong !!!')
} else {
result.innerText = evalConvert;
}
} else {
preview.innerText += myInput;
}
});
};