algebra
math
strings
Create a function that will handle simple math expressions. The input is an expression in the form of a string.
calculator("23+4") // 27
calculator("45-15") // 30
calculator("13+2-5*2") // 5
calculator("49/7*2-3") // 11
function calculator(str) {
return eval(str)
}
let Test = (function(){
return {
assertEquals:function(actual,expected){
if(actual !== expected){
let errorMsg = `actual is ${actual},${expected} is expected`;
throw new Error(errorMsg);
}
}
}
})();
Test.assertEquals(calculator("23+4"), 27)
Test.assertEquals(calculator("45-15"), 30)
Test.assertEquals(calculator("13+2-5*2"), 5)
Test.assertEquals(calculator("49/7*2-3"), 11)
Test.assertEquals(calculator("2+2*2"), 6)
Test.assertEquals(calculator("5/2"), 2.5)
Test.assertEquals(calculator("-34/4"), -8.5)
Test.assertEquals(calculator("0+43-0+56*0"), 43)
Test.assertEquals(calculator("-14*2-37-0"), -65)
Test.assertEquals(calculator("0*0"), 0)
Test.assertEquals(calculator("4+2+3-5*2-8/4-12-0+3-14"), -26)
Test.assertEquals(calculator("9/2+9/4"), 6.75)
Test.assertEquals(calculator("56*27*18*12/2*0"), 0)
Test.assertEquals(calculator("34/4*0*45*7"), 0)