diff --git a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/functional-programming/implement-map-on-a-prototype.md b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/functional-programming/implement-map-on-a-prototype.md index a3bbaf18554..f8addb15723 100644 --- a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/functional-programming/implement-map-on-a-prototype.md +++ b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/functional-programming/implement-map-on-a-prototype.md @@ -6,19 +6,21 @@ forumTopicId: 301230 --- ## Description +
As you have seen from applying Array.prototype.map(), or simply map() earlier, the map method returns an array of the same length as the one it was called on. It also doesn't alter the original array, as long as its callback function doesn't. In other words, map is a pure function, and its output depends solely on its inputs. Plus, it takes another function as its argument. -It would teach us a lot about map to try to implement a version of it that behaves exactly like the Array.prototype.map() with a for loop or Array.prototype.forEach(). -Note: A pure function is allowed to alter local variables defined within its scope, although, it's preferable to avoid that as well. +You might learn a lot about the map method if you implement your own version of it. It is recommended you use a for loop or Array.prototype.forEach().
## Instructions +
-Write your own Array.prototype.myMap(), which should behave exactly like Array.prototype.map(). You may use a for loop or the forEach method. +Write your own Array.prototype.myMap(), which should behave exactly like Array.prototype.map(). You should not use the built-in map method. The Array instance can be accessed in the myMap method using this.
## Tests +
```yml @@ -27,12 +29,12 @@ tests: testString: assert(JSON.stringify(new_s) === JSON.stringify([46, 130, 196, 10])); - text: Your code should not use the map method. testString: assert(!code.match(/\.?[\s\S]*?map/g)); - ```
## Challenge Seed +
@@ -41,34 +43,32 @@ tests: // The global variable var s = [23, 65, 98, 5]; -Array.prototype.myMap = function(callback){ +Array.prototype.myMap = function(callback) { var newArray = []; // Only change code below this line // Only change code above this line return newArray; - }; -var new_s = s.myMap(function(item){ +var new_s = s.myMap(function(item) { return item * 2; }); ```
- -
## Solution +
```js // the global Array var s = [23, 65, 98, 5]; -Array.prototype.myMap = function(callback){ +Array.prototype.myMap = function(callback) { var newArray = []; // Only change code below this line for (var elem of this) { @@ -76,10 +76,9 @@ Array.prototype.myMap = function(callback){ } // Only change code above this line return newArray; - }; -var new_s = s.myMap(function(item){ +var new_s = s.myMap(function(item) { return item * 2; }); ``` diff --git a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/functional-programming/implement-the-filter-method-on-a-prototype.md b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/functional-programming/implement-the-filter-method-on-a-prototype.md index d26c2961f1c..748c7b6e6c2 100644 --- a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/functional-programming/implement-the-filter-method-on-a-prototype.md +++ b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/functional-programming/implement-the-filter-method-on-a-prototype.md @@ -6,17 +6,19 @@ forumTopicId: 301231 --- ## Description +
-It would teach us a lot about the filter method if we try to implement a version of it that behaves exactly like Array.prototype.filter(). It can use either a for loop or Array.prototype.forEach(). -Note: A pure function is allowed to alter local variables defined within its scope, although, it's preferable to avoid that as well. +You might learn a lot about the filter method if you implement your own version of it. It is recommended you use a for loop or Array.prototype.forEach().
## Instructions +
-Write your own Array.prototype.myFilter(), which should behave exactly like Array.prototype.filter(). You may use a for loop or the Array.prototype.forEach() method. +Write your own Array.prototype.myFilter(), which should behave exactly like Array.prototype.filter(). You should not use the built-in filter method. The Array instance can be accessed in the myFilter method using this.
## Tests +
```yml @@ -25,12 +27,12 @@ tests: testString: assert(JSON.stringify(new_s) === JSON.stringify([23, 65, 5])); - text: Your code should not use the filter method. testString: assert(!code.match(/\.?[\s\S]*?filter/g)); - ```
## Challenge Seed +
@@ -39,44 +41,41 @@ tests: // The global variable var s = [23, 65, 98, 5]; -Array.prototype.myFilter = function(callback){ +Array.prototype.myFilter = function(callback) { // Only change code below this line var newArray = []; // Only change code above this line return newArray; - }; -var new_s = s.myFilter(function(item){ +var new_s = s.myFilter(function(item) { return item % 2 === 1; }); ```
- -
## Solution +
```js // the global Array var s = [23, 65, 98, 5]; -Array.prototype.myFilter = function(callback){ +Array.prototype.myFilter = function(callback) { var newArray = []; // Only change code below this line - for (let i = 0;i