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)

你可能感兴趣的:(#,Edabit,java,前端,服务器)