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)
相关推荐
她说..几秒前
Java Object类与String相关高频面试题
java·开发语言·jvm·spring boot·java-ee
Mr_Tony4 分钟前
Swift 中的 Combine 框架完整指南(含示例代码 + 实战)
开发语言·swift
计算机学姐6 分钟前
基于SpringBoot的宠物店管理系统
java·vue.js·spring boot·后端·spring·java-ee·intellij-idea
无心水7 分钟前
22、Java开发避坑指南:日期时间、Spring核心与接口设计的最佳实践
java·开发语言·后端·python·spring·java.time·java时间处理
Hello.Reader11 分钟前
双卡 A100 + Ollama 最终落地手册一键部署脚本、配置文件、预热脚本与 Python 客户端完整打包
开发语言·网络·python
Rsun0455117 分钟前
SpringBoot + Cursor 最佳提示词工程手册
java·spring boot·后端
cch891820 分钟前
汇编VS C++:底层控制与高效开发之争
java·开发语言
openallzzz20 分钟前
版本赶工期可临时扩容:模块开发、联调、交接一体化
java·摸鱼·外包
后端AI实验室25 分钟前
裁员后我被迫负责运维,用AI从0搭建了可观测性平台
java·ai
lifewange30 分钟前
代码托管平台
开发语言