0050【Edabit ★☆☆☆☆☆】【计算表达式】Evaluate an Equation

math numbers strings

Instructions

Create a function that evaluates an equation.

Examples
eq("1+2") // 3eq("6/(9-7)") // 3eq("3+2-4") // 1
Notes
  • Don’t print, return a value.
  • Return the value, not the equation.
Solutions
function eq(evaluate) { return eval(evaluate)}
TestCases
let Test = (function(){return {assertEquals:function(actual,expected){if(actual !== expected){let errorMsg = `actual is ${actual},${expected} is expected`;throw new Error(errorMsg);}},assertSimilar:function(actual,expected){if(actual.length != expected.length){throw new Error(`length is not equals, ${actual},${expected}`);}for(let a of actual){if(!expected.includes(a)){throw new Error(`missing ${a}`);}}}}})();Test.assertEquals(eq("1+2"), 3)Test.assertEquals(eq("6/(9-7)"), 3)Test.assertEquals(eq("3+2-4"), 1)Test.assertEquals(eq("3*4+1"), 13)Test.assertEquals(eq("5*8-4*9"), 4)Test.assertEquals(eq("3**7"), 2187)Test.assertEquals(eq("(6**3)+3"), 219)