-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
91 lines (77 loc) · 2.49 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
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
let firstNumber = 0;
let operator = "";
let secondNumber = 0;
let state = 0;
const clearButton = document.querySelector("#clear");
const numberButtons = document.querySelectorAll(".numberButton");
const operatorButtons = document.querySelectorAll(".operatorButton");
const display = document.querySelector("#display-txt");
let displayContent = ""
const add = function(a,b) {
return a+b;
}
const subtract = function(a,b) {
return a-b;
}
const multiply = function(a,b) {
return a*b;
}
const divide = function(a,b) {
return a/b;
}
const operation = function(operator, a, b) {
if (operator == "add") {
return add(a,b);
} else if (operator == "subtract") {
return subtract(a,b);
} else if (operator == "multiply") {
return multiply(a,b);
} else if (operator == "divide") {
return divide(a,b);
}
}
clearButton.addEventListener("click", () => {
displayContent = "0";
display.textContent = displayContent;
})
Array.from(numberButtons).forEach(button => {
button.addEventListener("click", () => {
if (button.textContent === '.' && displayContent.indexOf('.') !== -1) {
return;
}
if (displayContent.charAt(0) === '0' && (displayContent.indexOf('.') === -1 || button.textContent !== '.')) {
displayContent = displayContent.slice(1);
}
if (state === 0) {
displayContent = displayContent.concat(button.textContent);
firstNumber = Number(displayContent);
secondNumber = 0;
} else if (state === 1) {
displayContent = displayContent.concat(button.textContent);
secondNumber = Number(displayContent);
} else if (state === 2) {
displayContent = button.textContent;
firstNumber = Number(displayContent);
secondNumber = 0;
state = 0;
}
display.textContent = displayContent;
})
})
Array.from(operatorButtons).forEach(button => {
button.addEventListener("click", () => {
if (button.id !== "equal") {
operator = button.id;
displayContent = "0";
display.textContent = displayContent;
state = 1;
} else {
if (operator !== "") {
firstNumber = operation(operator, firstNumber, secondNumber);
state = 2;
displayContent = firstNumber.toString();
display.textContent = displayContent;
}
}
})
})