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)
相关推荐
CHANG_THE_WORLD8 分钟前
Python 字符串全面解析
开发语言·python
大怪v8 分钟前
【Virtual World 04】我们的目标,无限宇宙!!
前端·javascript·代码规范
不会c嘎嘎17 分钟前
深入理解 C++ 异常机制:从原理到工程实践
开发语言·c++
永远都不秃头的程序员(互关)37 分钟前
C语言 基本语法
c语言·开发语言
木鹅.1 小时前
LangChain4j
java
永远都不秃头的程序员(互关)1 小时前
Java核心技术精要:高效实践指南
java·开发语言·性能优化
是Dream呀1 小时前
Python圣诞特辑:打造一棵会唱歌、会下雪的魔法圣诞树
开发语言·python·pygame
未来之窗软件服务1 小时前
幽冥大陆(四十一)美萍V10酒店门锁SDK C#语言仙盟插件——东方仙盟筑基期
开发语言·c#·仙盟创梦ide·东方仙盟·东方仙盟sdk·酒店智能门锁·东方仙盟 vos 智能浏览器
CoderYanger2 小时前
动态规划算法-子序列问题(数组中不连续的一段):28.摆动序列
java·算法·leetcode·动态规划·1024程序员节
代码栈上的思考2 小时前
深入解析Spring IoC核心与关键注解
java·后端·spring