-
Notifications
You must be signed in to change notification settings - Fork 11
/
calc.js
72 lines (62 loc) · 3.44 KB
/
calc.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
// Define variables to store the input and result
let currentInput = '';
let currentResult = '';
// Function to append a digit or operator to the current input
function appendToInput(value) {
currentInput += value;
}
// Function to clear the input
function clearInput() {
currentInput = '';
}
// Function to calculate the result
function calculateResult() {
try {
currentResult = eval(currentInput);
} catch (error) {
currentResult = 'Error';
}
}
// Function to display the result
function displayResult() {
document.getElementById('output').textContent = currentResult;
}
// Add event listeners to digit and operator buttons
document.getElementById('button1').addEventListener('click', () => appendToInput('1'));
document.getElementById('button2').addEventListener('click', () => appendToInput('2'));
document.getElementById('button3').addEventListener('click', () => appendToInput('3'));
document.getElementById('button4').addEventListener('click', () => appendToInput('4'));
document.getElementById('button5').addEventListener('click', () => appendToInput('5'));
document.getElementById('button6').addEventListener('click', () => appendToInput('6'));
document.getElementById('button7').addEventListener('click', () => appendToInput('7'));
document.getElementById('button8').addEventListener('click', () => appendToInput('8'));
document.getElementById('button9').addEventListener('click', () => appendToInput('9'));
document.getElementById('button0').addEventListener('click', () => appendToInput('0'));
document.getElementById('add').addEventListener('click', () => appendToInput('+'));
document.getElementById('subtract').addEventListener('click', () => appendToInput('-'));
document.getElementById('multiply').addEventListener('click', () => appendToInput('*'));
document.getElementById('divide').addEventListener('click', () => appendToInput('/'));
document.getElementById('clear').addEventListener('click', () => clearInput());
document.getElementById('equals').addEventListener('click', () => {
calculateResult();
displayResult();
});
document.getElementById('button1').addEventListener('click', () => appendToInput('1'));
document.getElementById('button2').addEventListener('click', () => appendToInput('2'));
document.getElementById('button3').addEventListener('click', () => appendToInput('3'));
document.getElementById('button4').addEventListener('click', () => appendToInput('4'));
document.getElementById('button5').addEventListener('click', () => appendToInput('5'));
document.getElementById('button6').addEventListener('click', () => appendToInput('6'));
document.getElementById('button7').addEventListener('click', () => appendToInput('7'));
document.getElementById('button8').addEventListener('click', () => appendToInput('8'));
document.getElementById('button9').addEventListener('click', () => appendToInput('9'));
document.getElementById('button0').addEventListener('click', () => appendToInput('0'));
document.getElementById('add').addEventListener('click', () => appendToInput('+'));
document.getElementById('subtract').addEventListener('click', () => appendToInput('-'));
document.getElementById('multiply').addEventListener('click', () => appendToInput('*'));
document.getElementById('divide').addEventListener('click', () => appendToInput('/'));
document.getElementById('clear').addEventListener('click', () => clearInput());
document.getElementById('equals').addEventListener('click', () => {
calculateResult();
displayResult();
});