freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-an.../es6/write-higher-order-arrow-fu...

3.1 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
587d7b88367417b2b2512b45 Write Higher Order Arrow Functions 1 编写高阶箭头函数

Description

是时候我们看到处理数据时箭头功能有多强大了。 Arrow函数与高阶函数例如map() filter()reduce()非常兼容,它们将其他函数作为处理数据集合的参数。阅读以下代码:
FBPosts.filterfunctionpost{
return post.thumbnail== null && post.shares> 100 && post.likes> 500;
}
我们用filter()写了这个,至少使它有点可读。现在将它与以下使用箭头函数语法的代码进行比较:
FBPosts.filterpost=> 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