-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
139 lines (126 loc) · 3.28 KB
/
script.js
File metadata and controls
139 lines (126 loc) · 3.28 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//TO DO:
// 1. Add keyboard support
//operations functions
const add = (a, b) => a + b;
const subtract = (a, b) => a - b;
const multiply = (a, b) => a * b;
const divide = (a, b) => b != 0 ? a / b : "Error";
//variables for the calculator
let a = '';
let b = '';
let operator = '';
let result = '';
//function to round numbers only if not a int
function formatNumber(number) {
if (Number.isInteger(parseFloat(number))) {
return parseFloat(number).toString();
} else {
return parseFloat(number).toFixed(2);
}
}
const operate = function(operator, a, b) {
a = parseFloat(a);
b = parseFloat(b);
switch(operator) {
case '+':
result = add(a, b);
break;
case '-':
result = subtract(a, b);
break;
case '*':
result = multiply(a, b);
break;
case '/':
result = divide(a, b);
break;
default:
console.log('Invalid operator');
}
if (typeof result === 'number') {
result = formatNumber(result.toFixed(2));
}
}
//getting the values from the numbers
document.querySelectorAll('.number').forEach(item => {
item.addEventListener('click', e => {
if (operator === ''){
a += item.textContent;
} else {
b += item.textContent;
}
updateScreen();
});
});
//getting the operators
document.querySelectorAll('.operator').forEach(item => {
item.addEventListener('click', e => {
if (a !== '' && b !== '' && operator !== '') {
operate(operator, a, b);
a = result.toString();
b = '';
operator = item.textContent;
} else if (a !== '') {
operator = item.textContent;
}
updateScreen();
});
});
//get the .
document.querySelector('.dot').addEventListener('click', () => {
if (operator === "") {
//prevent that the user adds more than one .
if (!a.includes('.')) {
a += ".";
}
} else {
if (!b.includes('.')) {
b += ".";
}
}
updateScreen();
});
//using the %
document.querySelector('.percent').addEventListener('click', e => {
if (b === '' && a !== '') {
a = parseFloat(a) / 100;
updateScreen();
}
})
//using the = signal to start the calculations
document.querySelector('.equal').addEventListener('click', e => {
if (a != '' && b != '' && operator != '') {
operate(operator, a, b);
document.querySelector('.screen').textContent = result;
//reset the values for the next operation
a = result;
b = '';
operator = '';
updateScreen();
if (e) {
a = '';
}
}
})
//clear the screen
document.querySelector('.clear-all').addEventListener('click', e => {
a = '';
b = '';
operator = '';
updateScreen();
})
//delete button
document.querySelector('.delete').addEventListener('click', e => {
if (operator === '') {
a = a.slice(0, -1);
} else if (b === '') {
operator = '';
} else {
b = b.slice(0, -1);
}
updateScreen();
});
//updating the screen
function updateScreen() {
document.querySelector('.screen').textContent = a + (operator ? ` ${operator} ` : '') + b;
}