-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
53 lines (40 loc) · 1.13 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
let calculation = localStorage.getItem('calculation') || '';
let result = document.querySelector('.js-result')
// Display the calculation when the webpage loads
displayCalculation()
// Function for updating the calculation
function updateCalculation(value) {
calculation += value;
saveCalculate()
displayCalculation()
}
// Function to check if a character is an operator
function isOperator(char) {
return ['+', '-', '*', '/', '%'].includes(char);
}
// Function to perform backspace
function backspace() {
calculation = calculation.slice(0, -1); // Remove the last character
saveCalculate();
displayCalculation();
}
// Function to calculate the result
function calculateNumber() {
calculation = eval(calculation)
saveCalculate()
displayCalculation()
}
// Function to clear the calculation
function clearCalculate() {
calculation = '';
saveCalculate()
displayCalculation()
}
// Function to save the calculation on a localstorage
function saveCalculate() {
localStorage.setItem('calculation', calculation)
}
// Function to display the calculation
function displayCalculation() {
result.innerHTML = calculation
}