3.1 KiB
3.1 KiB
id, title, challengeType, videoUrl, localeTitle
id | title | challengeType | videoUrl | localeTitle |
---|---|---|---|---|
587d7b88367417b2b2512b45 | Write Higher Order Arrow Functions | 1 | 编写高阶箭头函数 |
Description
map()
, filter()
和reduce()
非常兼容,它们将其他函数作为处理数据集合的参数。阅读以下代码: FBPosts.filter(function(post){我们用
return post.thumbnail!== null && post.shares> 100 && post.likes> 500;
})
filter()
写了这个,至少使它有点可读。现在将它与以下使用箭头函数语法的代码进行比较: FBPosts.filter((post)=> post.thumbnail!== null && post.shares> 100 && post.likes> 500)此代码更简洁,使用更少的代码行完成相同的任务。
Instructions
realNumberArray
中只有正整数(十进制数不是整数)的realNumberArray
,并将新数组存储在变量squaredIntegers
。 Tests
tests:
- text: <code>squaredIntegers</code>应该是一个常量变量(通过使用<code>const</code> )。
testString: 'getUserInput => assert(getUserInput("index").match(/const\s+squaredIntegers/g), "<code>squaredIntegers</code> should be a constant variable (by using <code>const</code>).");'
- text: <code>squaredIntegers</code>应该是一个<code>array</code>
testString: 'assert(Array.isArray(squaredIntegers), "<code>squaredIntegers</code> should be an <code>array</code>");'
- text: '<code>squaredIntegers</code>应该是<code>[16, 1764, 36]</code> <code>squaredIntegers</code> <code>[16, 1764, 36]</code>'
testString: 'assert.deepStrictEqual(squaredIntegers, [16, 1764, 36], "<code>squaredIntegers</code> should be <code>[16, 1764, 36]</code>");'
- text: <code>function</code>关键字未使用。
testString: 'getUserInput => assert(!getUserInput("index").match(/function/g), "<code>function</code> keyword was not used.");'
- text: 不应该使用循环
testString: 'getUserInput => assert(!getUserInput("index").match(/(for)|(while)/g), "loop should not be used");'
- text: 应使用<code>map</code> , <code>filter</code>或<code>reduce</code>
testString: 'getUserInput => assert(getUserInput("index").match(/map|filter|reduce/g), "<code>map</code>, <code>filter</code>, or <code>reduce</code> should be used");'
Challenge Seed
const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2];
const squareList = (arr) => {
"use strict";
// change code below this line
const squaredIntegers = arr;
// change code above this line
return squaredIntegers;
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);
Solution
// solution required