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
javascript 复制代码
area(3, 4) // 12
area(10, 11) // 110
area(-1, 5) // -1
area(0, 2) // -1
Notes
  • N/A
Solutions
javascript 复制代码
function area(h, w) {
    if( h <= 0 || w <=0 ){
        return -1;
    }
    return  w*h;
}
TestCases
javascript 复制代码
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)
相关推荐
曾阿伦4 分钟前
Python3 文件 (夹) 操作备忘录
开发语言·python
清心歌5 分钟前
记一次系统环境变量更改后在IDEA中无法读取新值的排查过程
java·后端·intellij-idea·idea
大尚来也9 分钟前
驾驭并发:.NET多线程编程的挑战与破局之道
java·前端·算法
dong__csdn12 分钟前
jdk添加信任证书
java·开发语言
快乐小土豆~~16 分钟前
echarts柱状图的X轴label过长被重叠覆盖
前端·javascript·vue.js·echarts
hhcccchh25 分钟前
1.1 HTML 语义化标签(header、nav、main、section、footer 等)
java·前端·html
随风,奔跑28 分钟前
Spring Security
java·后端·spring
南 阳43 分钟前
Python从入门到精通day64
开发语言·python
小李子呢02111 小时前
前端八股2---Proxy 代理
前端·javascript·vue.js
yaaakaaang1 小时前
十二、代理模式
java·代理模式