1.7 KiB
1.7 KiB
id, title, challengeType, forumTopicId, dashedName
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
587d7b88367417b2b2512b44 | 編寫帶參數的箭頭函數 | 1 | 301223 | write-arrow-functions-with-parameters |
--description--
和一般的函數一樣,你也可以給箭頭函數傳遞參數。
const doubler = (item) => item * 2;
doubler(4);
doubler(4)
將返回 8
。
如果箭頭函數只有一個參數,則可以省略參數外面的括號。
const doubler = item => item * 2;
可以給箭頭函數傳遞多個參數。
const multiplier = (item, multi) => item * multi;
multiplier(4, 2);
multiplier(4, 2)
將返回 8
。
--instructions--
使用箭頭函數的語法重寫 myConcat
函數,將 arr2
的內容添加到 arr1
。
--hints--
應替換 var
關鍵詞。
(getUserInput) => assert(!getUserInput('index').match(/var/g));
myConcat
應該是一個常量(使用const
)。
(getUserInput) => assert(getUserInput('index').match(/const\s+myConcat/g));
myConcat
應該是一個帶有兩個參數的箭頭函數。
assert(
/myConcat=\(\w+,\w+\)=>/.test(code.replace(/\s/g, '')) &&
typeof myConcat === 'function'
);
myConcat()
應該返回 [1, 2, 3, 4, 5]
。
assert.deepEqual(myConcat([1, 2], [3, 4, 5]), [1, 2, 3, 4, 5]);
不能使用 function
關鍵字。
(getUserInput) => assert(!getUserInput('index').match(/function/g));
--seed--
--seed-contents--
var myConcat = function(arr1, arr2) {
return arr1.concat(arr2);
};
console.log(myConcat([1, 2], [3, 4, 5]));
--solutions--
const myConcat = (arr1, arr2) => {
return arr1.concat(arr2);
};
console.log(myConcat([1, 2], [3, 4, 5]));