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)
相关推荐
BothSavage3 分钟前
Ubuntu-8卡H20服务器升级nvidia驱动+cuda版本
linux·服务器·ubuntu·gpu·nvidia·cuda·nvcc
清风wxy6 分钟前
C语言基础数组作业(冒泡算法)
c语言·开发语言·数据结构·c++·windows·算法
IT小番茄6 分钟前
Kubernetes云平台管理实战:如何创建Deployment更好(九)
算法
白云千载尽23 分钟前
leetcode 2598 执行操作后最大MEX
算法·leetcode·职场和发展
岁月向前31 分钟前
网络数据大端序和小端序
算法
懒羊羊不懒@1 小时前
算法入门数学基础
c语言·数据结构·学习·算法
K_i1341 小时前
OSI七层模型:从原理到实战
运维·服务器·网络
信创工程师-小杨1 小时前
国产银河麒麟SP1桌面版本启动ssh服务报错解决办法
linux·服务器·ssh
mit6.8241 小时前
[Sora] 从检查点恢复训练 | `Booster`接口 | EMA模型 | .safetensors
人工智能·算法·机器学习
jixunwulian1 小时前
5G+AIoT智赋,AI电力加密边缘网关智慧电网数字化运维解决方案
运维·人工智能·5g