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)
相关推荐
记忆多8 分钟前
c++名字空间 函数模版 左右值
开发语言·c++·算法
lhbian16 分钟前
【Spring Cloud Alibaba】基于Spring Boot 3.x 搭建教程
java·spring boot·后端
雨雨雨雨雨别下啦41 分钟前
Vue案例——面经
前端·javascript·vue.js
2401_889884661 小时前
高性能计算通信库
开发语言·c++·算法
代码雕刻家1 小时前
3.6.Maven-依赖管理-依赖范围
java·maven
myNameGL2 小时前
ArkTs核心语法
前端·javascript·vue.js
范什么特西2 小时前
狂神报错页面设置
java·tomcat
浏览器API调用工程师_Taylor2 小时前
web逆向之小红书无水印图片提取工具
前端·javascript·逆向
架构师沉默2 小时前
AI 真的会取代程序员吗?
java·后端·架构
yuki_uix2 小时前
性能指标与优化:从 Core Web Vitals 到实战
前端·javascript