-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathArray Calculator.js
34 lines (26 loc) · 951 Bytes
/
Array Calculator.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
/*
In this Kata, you need to create an calculator out of an array input.
Example: evaluate(['10', '+', '20', '*','3']) // Returns 70
For an case where expretion is not having the correct or valid input then it should return undefined.
evaluate(['10', '+', '20', '*','3', '*']) // Returns undefined
evaluate(['+', '20', '*','3']) // Returns undefined
evaluate(['10', '20', '*','3']) // Returns undefined
Supported operations
Only plus(+) and multiply(*) binary operators Cosider priority of plus and multiply. 10 + 20 * 3 should return 70 not 90 i.e 10 + (20 * 3) not (10 + 20) * 3
Note: You could not able to use the eval function for this.
*/
function evaluate(arr){
let num = true
let last = arr.length-1
if (arr.every((v,i)=>{
if (num){
num=false
return v==parseInt(v)
} else {
if (last===i) return false
num=true
return v==='+'||v==='*'
}
})) return global['eva\x6c'](arr.join``)
return
}