-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
62 lines (56 loc) · 2.22 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
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
document.addEventListener('DOMContentLoaded', function() {
const display = document.getElementById('display');
const buttonsContainer = document.querySelector('.buttons');
const buttons = [
'7', '8', '9', '/', 'C',
'4', '5', '6', '*', '←',
'1', '2', '3', '-', '%',
'0', '.', '=', '+', '√',
'+/-', '^', '(', ')', 'End'
];
buttons.forEach(buttonText => {
const button = document.createElement('button');
button.textContent = buttonText;
button.classList.add('button');
if (buttonText === 'End') {
button.classList.add('end');
} else if (buttonText === 'C') {
button.classList.add('clear');
} else if (buttonText === '←') {
button.classList.add('backspace');
} else if (buttonText === '+/-') {
button.classList.add('special');
} else if (buttonText === '^' || buttonText === '(' || buttonText === ')') {
button.classList.add('grey');
} else if (buttonText === '%' || buttonText === '√') {
button.classList.add('operator');
}
button.addEventListener('click', () => {
onButtonClick(buttonText);
});
buttonsContainer.appendChild(button);
});
function onButtonClick(buttonText) {
if (buttonText === '=') {
try {
let currentText = display.value;
currentText = currentText.replace('^', '**');
currentText = currentText.replace('%', '*0.01');
const result = eval(currentText);
display.value = result;
} catch (error) {
display.value = 'Error';
}
} else if (buttonText === 'C') {
display.value = '';
} else if (buttonText === '←') {
display.value = display.value.slice(0, -1);
} else if (buttonText === '+/-') {
display.value = display.value.startsWith('-') ? display.value.slice(1) : '-' + display.value;
} else if (buttonText === 'End') {
window.close();
} else {
display.value += buttonText;
}
}
});