角形面积】Area of a Triangle"/>
【Edabit 算法 ★☆☆☆☆☆】【求三角形面积】Area of a Triangle
【Edabit 算法 ★☆☆☆☆☆】【求三角形面积】Area of a Triangle
math
numbers
geometry
Instructions
Write a function that takes the base and height of a triangle and return its area.
Examples
triArea(3, 2) // 3
triArea(7, 4) // 14
triArea(10, 10) // 50
Notes
- The area of a triangle is: (base * height) / 2
- Don’t forget to return the result.
Solutions
function triArea(base, height) {return base*height/2;
}
TestCases
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(triArea(3, 2), 3)
Test.assertEquals(triArea(5, 4), 10)
Test.assertEquals(triArea(10, 10), 50)
Test.assertEquals(triArea(0, 60), 0)
Test.assertEquals(triArea(12, 11), 66)
更多推荐
【Edabit 算法 ★☆☆☆☆☆】【求三角形面积】Area of a Triangle
发布评论