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)
相关推荐
passer__jw7674 分钟前
【LeetCode】【算法】283. 移动零
数据结构·算法·leetcode
Ocean☾11 分钟前
前端基础-html-注册界面
前端·算法·html
顶呱呱程序19 分钟前
2-143 基于matlab-GUI的脉冲响应不变法实现音频滤波功能
算法·matlab·音视频·matlab-gui·音频滤波·脉冲响应不变法
萨格拉斯救世主24 分钟前
戴尔R930服务器增加 Intel X710-DA2双万兆光口含模块
运维·服务器
无所谓จุ๊บ26 分钟前
树莓派开发相关知识十 -小试服务器
服务器·网络·树莓派
Jtti27 分钟前
Windows系统服务器怎么设置远程连接?详细步骤
运维·服务器·windows
爱吃生蚝的于勒40 分钟前
深入学习指针(5)!!!!!!!!!!!!!!!
c语言·开发语言·数据结构·学习·计算机网络·算法
yeyuningzi41 分钟前
Debian 12环境里部署nginx步骤记录
linux·运维·服务器
羊小猪~~44 分钟前
数据结构C语言描述2(图文结合)--有头单链表,无头单链表(两种方法),链表反转、有序链表构建、排序等操作,考研可看
c语言·数据结构·c++·考研·算法·链表·visual studio
王哈哈^_^1 小时前
【数据集】【YOLO】【VOC】目标检测数据集,查找数据集,yolo目标检测算法详细实战训练步骤!
人工智能·深度学习·算法·yolo·目标检测·计算机视觉·pyqt