-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
58 lines (43 loc) · 2.42 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
let displayValue = document.querySelector("#display").innerText; //On the display screen; will be used thoughout
let holder = []; //Stores the previous value(s)
let calculateString = ""; //Will loop holder values into this one string
function press(value){
if (displayValue == 0){
displayValue = ""; //Ensures that 0 will not be in the front of our value
}
if (holder[holder.length -1] !== "+" && holder[holder.length -1] !== "-" && //Used to see if the holder's stored value needs to be reset after a value is calculated
holder[holder.length -1] !== "*" && holder[holder.length -1] !== "/"){
holder = [];
}
displayValue = "" + displayValue + value;
document.querySelector("#display").innerText = displayValue; //Turns the number into a string and displays it
}
function clr(){
displayValue = 0; //Clears everything
holder = [];
calculateString = "";
document.querySelector("#display").innerText = displayValue;
}
function setOP(value){ //The IF statement allows us to keep going after calculating and makes our holder array clean
if (displayValue === ""){
holder.push(value)
}else {
holder.push(displayValue, value); //Pushes value and operator into the array
}
console.log(holder);
displayValue = 0;
}
function calculate(){
holder.push(displayValue); //Pushes last value into holder bc it will not be picked up ny setOP
console.log(holder);
for (let i = 0; i < holder.length; i++){
calculateString += holder[i]; //Array -> string
}
console.log(calculateString);
displayValue = Math.round(eval(calculateString) * 1000000) / 1000000 //Calculate the string and round it to 6 decimals
document.querySelector("#display").innerText = displayValue;
holder = [displayValue]; //Stores value so we can keep calculating
calculateString = ""; //resets displayValue and string after calculation
displayValue = "";
console.log(displayValue);
}