0062【Edabit ★☆☆☆☆☆】Arrow Functions

0062【Edabit ★☆☆☆☆☆】Arrow Functions

closures higher_order_functions language_fundamentals logic

Instructions

In the Code tab you will find code that is missing a single character in order to pass the tests. However, your goal is to submit a function as minimalist as possible. Use the tips in the tips section below.

Write five adder functions:

  • add2(x) should return 2 + x.
  • add3(x) should return 3 + x.
  • add5(x) should return 5 + x.
  • add7(x) should return 7 + x.
  • add11(x) should return 11 + x.
Tips

Functions that consist only of a return can be written as a one-liner with an arrow function.

For example, the code:

javascript 复制代码
function areSame(a, b) {
   return a == b;
}

Can be simplified to:

javascript 复制代码
areSame = (a, b) => a == b;
Bonus

(a, b) => a == b is technically an anonymous function. However, it can be assigned to the identifier areSame and it can then be called using areSame().

Notes
  • N/A
Solutions
javascript 复制代码
function add2(x) {
	return x + 2;
}

function add3(x) {
	return x + 3;
}

function add5(x) {
	return x + 5;
}

function add7(x) {
	return x + 7;
}

function add11(x) {
	return x + 1;
}
// 
add2 = (x) => x + 2;
add3 = (x) => x + 3;
add5 = (x) => x + 5;
add7 = (x) => x + 7;
add11 = (x) => x + 11;
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(add2(1), 3)
Test.assertEquals(add2(9), 11)
Test.assertEquals(add2(435), 437)

Test.assertEquals(add3(1), 4)
Test.assertEquals(add3(9), 12)
Test.assertEquals(add3(435), 438)

Test.assertEquals(add5(1), 6)
Test.assertEquals(add5(9), 14)
Test.assertEquals(add5(435), 440)

Test.assertEquals(add7(1), 8)
Test.assertEquals(add7(9), 16)
Test.assertEquals(add7(435), 442)

Test.assertEquals(add11(1), 12)
Test.assertEquals(add11(9), 20)
Test.assertEquals(add11(435), 446)
相关推荐
Doro再努力3 分钟前
【Linux05】Linux权限管理深度解析(二)
linux·运维·服务器
阿猿收手吧!4 分钟前
【C++】C++模板特化:精准定制泛型逻辑
开发语言·c++·算法
智驱力人工智能18 分钟前
货车走快车道检测 高速公路安全治理的工程实践与价值闭环 高速公路货车占用小客车道抓拍系统 城市快速路货车违规占道AI识别
人工智能·opencv·算法·安全·yolo·目标检测·边缘计算
喵手28 分钟前
Python爬虫实战:电商实体消歧完整实战 - 从混乱店铺名到标准化知识库的工程化实现,一文带你搞定!
爬虫·python·算法·爬虫实战·零基础python爬虫教学·同名实体消除·从混乱店铺名到标准化知识库
weixin_4521595532 分钟前
C++与Java性能对比
开发语言·c++·算法
80530单词突击赢32 分钟前
C++哈希表实现:开散列与闭散列详解
算法·哈希算法·散列表
Timmylyx051835 分钟前
类欧几里得学习笔记
笔记·学习·算法
wangluoqi38 分钟前
26.2.2练习总结
算法
2301_7657031439 分钟前
C++中的工厂模式实战
开发语言·c++·算法
鱼跃鹰飞39 分钟前
Leetcode:97.交错字符串
linux·服务器·leetcode