本文主要是介绍0046【Edabit ★☆☆☆☆☆】【长方形面积】Area of a Rectangle,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
0046【Edabit ★☆☆☆☆☆】【长方形面积】Area of a Rectangle
algebra
geometry
math
Instructions
Create a function that calculates the area of a rectangle. If the arguments are invalid, your function must return
-1
.
Examples
area(3, 4) // 12
area(10, 11) // 110
area(-1, 5) // -1
area(0, 2) // -1
Notes
- N/A
Solutions
function area(h, w) {if( h <= 0 || w <=0 ){return -1;}return w*h;
}
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(area(5, 3), 15)
Test.assertEquals(area(8, 5), 40)
Test.assertEquals(area(5, 4), 20)
Test.assertEquals(area(2, 3), 6)
Test.assertEquals(area(10000, 10000), 100000000)
Test.assertEquals(area(-2, -5), -1)
Test.assertEquals(area(0, 3), -1)
Test.assertEquals(area(5, -3), -1)
Test.assertEquals(area(0, 1), -1)
Test.assertEquals(area(-1, 0), -1)
这篇关于0046【Edabit ★☆☆☆☆☆】【长方形面积】Area of a Rectangle的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!