-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc.js
53 lines (48 loc) · 1.24 KB
/
calc.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
runningTotal = [];
opperatorsUsed = [];
opperators = "+-/*np";
total = 0;
button = "/";
runningTotal.push("100");
runningTotal.push("200");
runningTotal.push("200");
console.log(runningTotal);
//compare the opperators use array to the opperatiors table
// n will be used to convert to negative 1 --> (-1)
// p will be used to convert to positive (-1) --> 1
// Turn this into a recursion function less reuse of code
//Addition
if (button == "+") {
total = parseFloat(runningTotal[0]);
for (i = 1; i < runningTotal.length; i++) {
number = parseInt(runningTotal[i]);
total = total + number;
}
}
//Multipulcation
else if (button == "*") {
total = parseFloat(runningTotal[0]);
for (i = 1; i < runningTotal.length; i++) {
number = parseFloat(runningTotal[i]);
total = total * number;
}
}
//Subtraction
else if (button == "-") {
total = parseFloat(runningTotal[0]);
for (i = 1; i < runningTotal.length; i++) {
number = parseInt(runningTotal[i]);
total = total - number;
}
}
//Division
else if (button == "/") {
total = parseFloat(runningTotal[0]);
for (i = 1; i < runningTotal.length; i++) {
number = parseInt(runningTotal[i]);
total = total / number;
}
} else {
console.log("not detected");
}
console.log(total);