-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
115 lines (108 loc) · 2.28 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
/*
* @author roop kadam
@description calculator code
*/
var expression="";
var val;
/**@function one(val)
@description fetching the numbers
@param {array} val
@return {array} expression
containing numbers and operators
*/
function one(val) {
document.getElementById('text1').value += val;
if (val == '+' || val == '*' || val == '-' || val == '/') {
expression = expression + "|" + val + "|";
}
else{
expression += val;
}
}
/**
@function textClear
@description used to clear user's input text box
*/
function textClear()
{
document.getElementById('text1').value = "";
expression="";
}
/**
@function equal
@description used for splitting array
and passing it to calculate()
*/
function equal() {
expArr = expression.split("|");
calculate(expArr);
}
/**
@function calculate()
@description
@param {array} expArr
*/
function calculate(expArr) {
console.log(expArr);
let index = 0;
let answer = 0;
//Division for each function
expArr.forEach(function (number) {
console.log(number);
if (number == "/")
{
number1 = index - 1;
number2 = index +1;
answer = parseInt(expArr[number1]) / parseInt(expArr[number2]);
expArr.splice(index,2);
expArr[number1]=answer.toString();
}
index +=1;
});
index=0;
//Multiplication for each function
expArr.forEach(function (number)
{
debugger;
console.log(number);
if (number == "*")
{
number1 = index - 1;
number2 = index +1;
answer = parseInt(expArr[number1]) * parseInt(expArr[number2]);
expArr.splice(index,2);
expArr[number1]=answer.toString();
}
index +=1;
});
index = 0;
//Addition for each function
expArr.forEach(function (number) {
console.log(number);
if (number == "+")
{
number1 = index - 1;
number2 = index +1;
answer = parseInt(expArr[number1]) + parseInt(expArr[number2]);
expArr.splice(index,2);
expArr[number1]=answer.toString();
}
index +=1;
});
index = 0;
//Substraction for each function
expArr.forEach(function (number) {
console.log(number);
if (number == "-")
{
number1 = index - 1;
number2 = index +1;
answer = parseInt(expArr[number1]) - parseInt(expArr[number2]);
expArr.splice(index,2);
expArr[number1]=answer.toString();
}
index +=1;
});
index = 0;
document.getElementById('text1').value = +answer;
}