本文主要是介绍0051【Edabit ★☆☆☆☆☆】【解析表达式】Solve the Equation,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
0051【Edabit ★☆☆☆☆☆】【解析表达式】Solve the Equation
language_fundamentals
math
Instructions
Create a function that takes an equation (e.g.
"1+1"
), and returns the answer.
Examples
equation("1+1") // 2
equation("7*4-2") // 26
equation("1+1+1+1+1") // 5
Notes
- Supported operators are
+
,-
, and*
.
Solutions
function equation(s) {return eval(s)
}
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(equation("1+1"), 2)
Test.assertEquals(equation("7*4-2"), 26)
Test.assertEquals(equation("1+1+1+1+1"), 5)
这篇关于0051【Edabit ★☆☆☆☆☆】【解析表达式】Solve the Equation的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!