-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
57 lines (51 loc) · 1.38 KB
/
app.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
$(() => {
let value = 0;
let savedValue = 0;
let operator = '';
// Display output
const output = (param) => {
$('.output p').text(param);
}
// Reset values
const reset = () => {
value = 0;
savedValue = 0;
output(value);
}
// Stores value of number pressed
const numberPress = (event) => {
if (value === 0) {
value = $(event.currentTarget).text();
output(value)
} else if (value !== 0) {
value += $(event.currentTarget).text();
output(value)
}
}
// Check operator pressed and stores value
const operatorPress = (event) => {
operator = $(event.currentTarget).text();
savedValue = value;
value = 0;
}
// Calculates final value
const calculator = () => {
if (operator === '+') {
let finalValue = parseInt(savedValue) + parseInt(value)
output(finalValue);
} else if (operator === '-') {
let finalValue = parseInt(savedValue) - parseInt(value)
output(finalValue);
} else if (operator === '*') {
let finalValue = parseInt(savedValue) * parseInt(value)
output(finalValue);
} else if (operator === '/') {
let finalValue = parseInt(savedValue) / parseInt(value)
output(finalValue);
}
}
$('.operator').on('click', operatorPress);
$('.number').on('click', numberPress);
$('.equals').on('click', calculator);
$('.reset').on('click', reset);
});