-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
136 lines (124 loc) · 4.56 KB
/
script.js
File metadata and controls
136 lines (124 loc) · 4.56 KB
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
126
127
128
129
130
131
132
133
134
135
136
var num = document.querySelectorAll(".num div");//all the num buttons
var operator = document.querySelectorAll(".symbol div");//all the operator
var display = document.getElementById("display");//display div
var result = document.getElementById("equal");//equal btn
var clear = document.getElementById("clear");//clear all button
var resultDisplayed = false;//keeps the track whether the result has been displayed
//add click listeners to the number btns
for(var i = 0 ; i < num.length ; i++)
{
num[i].addEventListener("click", function(e)
{
/** if currently there is no result displayed then,
add the pressed number to the display div
* if the result has been displayed then,
see if the last entered character is a symbol then continue to ask for a number
clear the display and then add the number to the div
*/
var currentString = display.innerHTML;
var lastChar = currentString[currentString.length - 1];
if(resultDisplayed == false)
{
display.innerHTML += e.target.innerHTML;
}
else if(lastChar == "+" || lastChar == "-" || lastChar == "×" || lastChar == "÷")
{
resultDisplayed = false;
display.innerHTML += e.target.innerHTML;
}
else
{
resultDisplayed = false;
display.innerHTML = "";
display.innerHTML += e.target.innerHTML;
}
});
}
//add click listeners to the operator btns
for(var i = 0 ; i < operator.length ; i++ )
{
operator[i].addEventListener("click", function(e)
{
/** if a character has been pressed then,
replace the character with the current
* if a character is pressed without any number,
ignore it
* if any of the above case has not occured
add it to the input string
*/
var currentString = display.innerHTML;
var lastChar = currentString[currentString.length - 1];
if(lastChar == "+" || lastChar == "-" || lastChar == "×" || lastChar == "÷")
{
var newString = currentString.substring(0,currentString.length - 1) + e.target.innerHTML;
display.innerHTML = newString;
}
else if(currentString.length == 0)
{
window.alert("Enter an operand first!!");
}
else
{
display.innerHTML += e.target.innerHTML;
}
});
}
//when the equals btn is pressed
result.addEventListener("click", function()
{
var inputString = display.innerHTML;
var lastChar = inputString[inputString.length - 1];
//if the last entered character was a operator ask for the missing operand
if(lastChar === "+" || lastChar == "-" || lastChar == "×" || lastChar == "÷")
{
window.alert("Enter an operand after an operator!!");
}
else
{
var numbers = inputString.split(/\+|\-|\÷|\×/g);//forming an array of numbers
var op = inputString.replace(/[0-9]|\./g, "").split("");//forming an array of operators
console.log(numbers);
console.log(op);
//performing the arithmetic according to the BODMAS
//each of these while blocks execute the operations and replaces the two operands in the numbers array with the result
var div = op.indexOf("÷");
while(div!=-1)
{
numbers.splice(div,2,numbers[div]/numbers[div+1]);
op.splice(div,1);
div = op.indexOf("÷");
}
console.log(numbers);
var multi = op.indexOf("×");
while(multi!=-1)
{
numbers.splice(multi,2,numbers[multi]*numbers[multi+1]);
op.splice(multi,1);
multi = op.indexOf("×");
}
console.log(numbers);
var sub = op.indexOf("-");
while(sub!=-1)
{
numbers.splice(sub,2,numbers[sub]-numbers[sub+1]);
op.splice(sub,1);
sub = op.indexOf("-");
}
console.log(numbers);
var add = op.indexOf("+");
while(add!=-1)
{
numbers.splice(add,2,parseFloat(numbers[add])+parseFloat(numbers[add+1]));
op.splice(add,1);
add = op.indexOf("+");
}
console.log(numbers);
display.innerHTML = "";
display.innerHTML = numbers[0];
resultDisplayed = true;
}
});
clear.addEventListener("click",function()
{
display.innerHTML = "";
});