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)
相关推荐
第1缕阳光3 分钟前
Java垃圾回收机制和三色标记算法
java·jvm
funnyZpC25 分钟前
好用的文档工具👉smart-doc
java
清幽竹客32 分钟前
vue-30(理解 Nuxt.js 目录结构)
前端·javascript·vue.js
一只叫煤球的猫32 分钟前
🔥 同事混用@Transactional和TransactionTemplate被我怼了,三种事务管理到底怎么选?
java·spring boot·后端
weiweiweb88833 分钟前
cesium加载Draco几何压缩数据
前端·javascript·vue.js
我不吃饼干9 天前
鸽了六年的某大厂面试题:你会手写一个模板引擎吗?
前端·javascript·面试
李少兄9 天前
解决OSS存储桶未创建导致的XML错误
xml·开发语言·python
阿蒙Amon9 天前
《C#图解教程 第5版》深度推荐
开发语言·c#
我不吃饼干9 天前
鸽了六年的某大厂面试题:手写 Vue 模板编译(解析篇)
前端·javascript·面试
学Linux的语莫9 天前
python基础语法
开发语言·python