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)
相关推荐
kyle~21 分钟前
C++_STL---迭代器失效
开发语言·c++
谢亮_vipxieliang1 小时前
ValidX分组验证详解:不同场景使用不同验证规则
java·spring boot·后端·spring cloud·eclipse·hibernate
Brilliantwxx1 小时前
【C语言】 初入嵌入式C语言复习(基础+进阶面试题)
c语言·开发语言
熊野君2 小时前
附录与 Codex 实操手册
开发语言·人工智能·产品经理
wuminyu2 小时前
深入剖析 Panama Off-heap 的性能损耗与开销
java·linux·c语言·jvm·c++
默_笙2 小时前
🔥 让 AI 帮我写完整个 Next.js 博客:框架就是 AI 的"上下文 buff"
前端·javascript
pnoker2 小时前
IoT DC3 AI 能力:Agentic Center 的设计与边界
java·人工智能·物联网·大模型·spring ai
xiaoqiMikko2 小时前
一条 CVSS 9.0 的 RCE,advisory 里没写修复版是哪个 —— fastjson 16723 的字段考古
java·安全
程序员贺加贝2 小时前
一次 SaaS ERP 主数据生命周期设计:Policy、PreCheck 与结构化阻断原因
java·后端·架构·saas
XZ-0700013 小时前
week2-1-可视化
开发语言·python