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)此代码更简洁,使用更少的代码行完成相同的任务。
realNumberArray
中只有正整数(十进制数不是整数)的realNumberArray
,并将新数组存储在变量squaredIntegers
。 squaredIntegers
应该是一个常量变量(通过使用const
)。
testString: 'getUserInput => assert(getUserInput("index").match(/const\s+squaredIntegers/g), "squaredIntegers
should be a constant variable (by using const
).");'
- text: squaredIntegers
应该是一个array
testString: 'assert(Array.isArray(squaredIntegers), "squaredIntegers
should be an array
");'
- text: 'squaredIntegers
应该是[16, 1764, 36]
squaredIntegers
[16, 1764, 36]
'
testString: 'assert.deepStrictEqual(squaredIntegers, [16, 1764, 36], "squaredIntegers
should be [16, 1764, 36]
");'
- text: function
关键字未使用。
testString: 'getUserInput => assert(!getUserInput("index").match(/function/g), "function
keyword was not used.");'
- text: 不应该使用循环
testString: 'getUserInput => assert(!getUserInput("index").match(/(for)|(while)/g), "loop should not be used");'
- text: 应使用map
, filter
或reduce
testString: 'getUserInput => assert(getUserInput("index").match(/map|filter|reduce/g), "map
, filter
, or reduce
should be used");'
```