-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
125 lines (103 loc) · 3.07 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
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
const allNumButtons = document.getElementsByClassName("number");
const allOperatorButtons = document.getElementsByClassName("operator");
const equalButton = document.querySelector(".equals");
const display = document.querySelector(".display");
const clearButton = document.querySelector(".clear");
for (i=0 ; i<allNumButtons.length ; i++){
allNumButtons[i].addEventListener("click", setValues);
}
for (i=0 ; i<allOperatorButtons.length ; i++){
allOperatorButtons[i].addEventListener("click", setOperator);
}
equalButton.addEventListener("click", execute);
clearButton.addEventListener("click", clear);
let valueOne = "";
let valueTwo = "";
let savedValue = false;
let operatorSelection = "";
let displayResult = 0;
let equalJustPressed = false;
let dotPressed = false;
function convertValue(text){
return parseFloat(text);
}
function setValues(){
if (equalJustPressed){
valueOne = "";
valueTwo = "";
savedValue = false;
equalJustPressed = false;
}
if (dotPressed && this.textContent =="."){
console.log("Multiple '.' ignored")
} else {
if (this.textContent == "."){
dotPressed = true;
}
if (!savedValue){
valueOne += (this.textContent);
updateDisplay(valueOne);
console.log(`Value one: ${valueOne}`);
} else {
valueTwo += (this.textContent);
updateDisplay(valueTwo);
console.log(`Value two: ${valueTwo}`);
}
}
}
function setOperator(){
if (equalJustPressed){
valueTwo = "";
}
operatorSelection = this.textContent;
console.log(`Operator: ${operatorSelection}`);
savedValue = true;
equalJustPressed = false;
dotPressed = false;
console.log(valueOne,valueTwo);
}
function execute(){
console.log(valueOne,valueTwo)
if (operatorSelection == "x"){
displayResult = valueOne * valueTwo;
} else if (operatorSelection == "/"){
displayResult = valueOne / valueTwo;
} else if (operatorSelection == "+"){
displayResult = convertValue(valueOne) + convertValue(valueTwo);
} else if (operatorSelection == "-"){
displayResult = valueOne - valueTwo;
}
updateDisplay(displayResult);
console.log(`Result = ${displayResult}`);
valueOne = displayResult;
equalJustPressed = true;
dotPressed = false;
}
function updateDisplay(value){
display.textContent = value;
}
function clear(){
valueOne = "";
valueTwo = "";
savedValue = false;
operatorSelection = "";
displayResult = 0;
equalJustPressed = false;
dotPressed = false;
updateDisplay(displayResult);
console.log("-- CLEAR --");
}
// Adding continuous functionality:
// Save result to valueOne after equal sign is pressed
// If the operator is pressed continue as normal
// If a number is pressed, start over on valueOne
// Pseudo:
// start
// click number
// save valueOne
// Click operator
// save operatorValue
// Click number
// save ValueTwo
// click Equals sign
// Update display to result, save result as valueOne