map()
, filter()
, and reduce()
. forEach()
, and run it on the following array of sample Facebook posts.
-let FBPosts = [-Of the two
- {thumbnail: "someIcon", likes:432, shares: 600},
- {thumbnail: "Another icon", likes:300, shares: 501},
- {thumbnail: "Yet another", likes:40, shares: 550},
- {thumbnail: null, likes: 101, shares:0},
-] -
forEach()
versions below, both perform the exact same log function, and each takes an anonymous callback with a parameter post
. The difference is the syntax. One uses an arrow function and the other does not.
--ES5-
-FBpost.forEach(function(post) {
- console.log(post) // log each post here
- });
-ES6
-FBpost.forEach((post) => {
- console.log(post) // log each post here
- });
- -
filter()
is very similar. Below it will iterate over the FBPosts
array, perform the logic to filter out the items that do not meet the requirements, and return a new array, results
.
-
--let results = arr1.filter((post) => { - return post.thumbnail !== null && post.likes > 100 && post.shares > 500 -});- -
-console.log(results); // [{thumbnail: "someIcon", likes: 432, shares: 600}, {thumbnail: "Another icon", likes: 300, shares: 501}] -
realNumberArray
and store the new array in the variable squaredIntegers
.
-squareList
should be a function
.
- testString: assert.typeOf(squareList, 'function'), 'squareList
should be a function
';
- - text: squareList
should be a constant variable (by using const
).
- testString: getUserInput => assert(getUserInput('index').match(/const\s+squaredIntegers/g), 'squaredIntegers
should be a constant variable (by using const
).');
- - text: function
keyword was not used.
- testString: getUserInput => assert(!getUserInput('index').match(/function/g), 'function
keyword was not used.');
- - text: loop should not be used
- testString: getUserInput => assert(!getUserInput('index').match(/(for)|(while)/g), 'loop should not be used');
- - text: map
, filter
, or reduce
should be used
- testString: getUserInput => assert(getUserInput('index').match(/map|filter|reduce/g), 'map
, filter
, or reduce
should be used');
- - text: The function should return an array
called squaredIntegers
- testString: assert(Array.isArray(squaredIntegers), 'squaredIntegers
should be an array
');
- - text: squaredIntegers
should be [16, 1764, 36]
- testString: assert.deepStrictEqual(squaredIntegers, [16, 1764, 36], 'squaredIntegers
should be [16, 1764, 36]
');
-
-```
-
-map()
, filter()
, and reduce()
, you now get to apply them to solve a more complex challenge.
+squareList
. You need to complete the code for the squareList
function using any combination of map()
, filter()
, and reduce()
so that it returns a new array containing only the square of only the positive integers (decimal numbers are not integers) when an array of real numbers is passed to it. An example of an array containing only real numbers is [-3, 4.8, 5, 3, -3.2]
.
+Note: Your function should not use any kind of for
or while
loops or the forEach()
function.
+squareList
should be a function
.
+ testString: assert.typeOf(squareList, 'function'), 'squareList
should be a function
';
+ - text: for or while loops or forEach should not be used.
+ testString: assert(!removeJSComments(code).match(/for|while|forEach/g));
+ - text: map
, filter
, or reduce
should be used.
+ testString: assert(removeJSComments(code).match(/\.(map|filter|reduce)\s*\(/g));
+ - text: The function should return an array
.
+ testString: assert(Array.isArray(squareList([4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2])));
+ - text: squareList([4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2])
should return [16, 1764, 36]
.
+ testString: assert.deepStrictEqual(squareList([4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2]), [16, 1764, 36]);
+ - text: squareList([-3.7, -5, 3, 10, 12.5, 7, -4.5, -17, 0.3])
should return [9, 100, 49]
.
+ testString: assert.deepStrictEqual(squareList([-3.7, -5, 3, 10, 12.5, 7, -4.5, -17, 0.3]), [9, 100, 49]);
+```
+
+