-
Notifications
You must be signed in to change notification settings - Fork 0
/
calci.js
62 lines (38 loc) · 1.27 KB
/
calci.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
var a = '';
var b = '';
var num = [];
var ans;
// All the numbers and operators input will be stored in an array "num" using function "sendNum()"
function sendNum(digit){
num.push(digit);
if(num.length != 1){
a = '';
document.getElementById('screen').innerHTML = a; // clearing the screen.
}
for(i=0; i<num.length ; i++){
a = a + num[i]; // concatenate the elements of the array "num" into a single string, which will be displayed on the screen
}
document.getElementById('screen').innerHTML = a; // displaying the concatenated string
}
// when the user presses "=", function "equalTo()" is called
function equalTo(){
document.getElementById('screen').innerHTML = '';
for(i=0; i<num.length ; i++){
b += num[i]; // concatenating the array "num" into a single string
}
ans = eval(b);
document.getElementById('screen').innerHTML = ans; // result display
while(num.length > 0){
num.pop(); // emptying the array "num"
}
num.push(ans.toString());
}
// When user presses "AC", function "clearScr()" is called
function clearScr(){
document.getElementById('screen').innerHTML = '';
while(num.length > 0){
num.pop(); // emptying the array "num"
}
a ='';
b ='';
}