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)
相关推荐
梦回江东6 分钟前
递归的编译优化(2)
算法
不会C语言的男孩9 分钟前
TCP通信可视化工具,含下载地址
服务器·网络·安全
学究天人10 分钟前
数学公理体系大全:第六章 选择公理的等价形式及证明
人工智能·线性代数·算法·机器学习·数学建模·概率论·原型模式
:-)16 分钟前
基础算法-插入排序
数据结构·算法·排序算法
gumichef19 分钟前
排序专题(插入,)
数据结构·算法·排序算法
初学者,亦行者23 分钟前
算法设计与分析3:贪心法 - 求解最短路径问题(TSP)
算法·代理模式
Starmoon_dhw28 分钟前
题解:P16108 「o.OI R-1」基础博弈练习题
c++·算法·图论
大鱼>41 分钟前
AI+快递分拣:视觉识别+自动分拣+异常检测
人工智能·深度学习·算法·机器学习
想要成为糕糕手43 分钟前
238. 除了自身以外数组的乘积 — 面试向深度解析
javascript·算法·面试
玖玥拾44 分钟前
C# 语言进阶(十三)网络 DLL 库分层设计
服务器·开发语言·网络·网络协议·c#