68 lines
		
	
	
		
			4.1 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
		
		
			
		
	
	
			68 lines
		
	
	
		
			4.1 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
|   | --- | ||
|  | title: Write Higher Order Arrow Functions | ||
|  | --- | ||
|  |  | ||
|  | 
 | ||
|  |  Remember to use <a>**`Read-Search-Ask`**</a> if you get stuck. Try to pair program  and write your own code  | ||
|  | 
 | ||
|  | ### Problem Explanation:
 | ||
|  | 
 | ||
|  | We need to compute and square values from the `realNumberArray` and store them in the variable `squaredIntegers` using the `map()`, `filter()`, and/or `reduce()` functions. | ||
|  | 
 | ||
|  | ##  Hint: 1
 | ||
|  | 
 | ||
|  | *   You will need to `filter()` the `realNumberArray` for positive integers (decimals are not integers). | ||
|  | 
 | ||
|  | > _try to solve the problem now_
 | ||
|  | 
 | ||
|  | ##  Hint: 2
 | ||
|  | 
 | ||
|  | *   You will need to `map()` the values from your `filter()` function to the variable `squaredIntegers`. | ||
|  | 
 | ||
|  | > _try to solve the problem now_
 | ||
|  | 
 | ||
|  | ##  Hint: 3
 | ||
|  | 
 | ||
|  | *   Remember the magic of chaining functions. | ||
|  | 
 | ||
|  | > _try to solve the problem now_
 | ||
|  | 
 | ||
|  | ## Spoiler Alert!
 | ||
|  | 
 | ||
|  |  | ||
|  | 
 | ||
|  | **Solution ahead!** | ||
|  | 
 | ||
|  | ##  Basic Code Solution:
 | ||
|  | ```javascript | ||
|  |     const squareList = (arr) => { | ||
|  |       "use strict"; | ||
|  |       const squaredIntegers = arr.filter( (num) => num > 0 && num % parseInt(num) === 0 ).map( (num) => Math.pow(num, 2) ); | ||
|  |       return squaredIntegers; | ||
|  |     }; | ||
|  | 
 | ||
|  |     // test your code | ||
|  |     const squaredIntegers = squareList(realNumberArray); | ||
|  |     console.log(squaredIntegers); | ||
|  | ``` | ||
|  |  <a href='https://codepen.io/dylantyates/pen/WyWoYJ' target='_blank' rel='nofollow'>Run Code</a> | ||
|  | 
 | ||
|  | # Code Explanation:
 | ||
|  | 
 | ||
|  | Uses the operator `filter()` and `map()` functions to square all positive integers in a given array. | ||
|  | 
 | ||
|  | #### Relevant Links
 | ||
|  | 
 | ||
|  | *   <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map' target='_blank' rel='nofollow'>map()</a> | ||
|  | *   <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter' target='_blank' rel='nofollow'>filter()</a> | ||
|  | *   <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce' target='_blank' rel='nofollow'>reduce()</a> | ||
|  | 
 | ||
|  | ##  NOTES FOR CONTRIBUTIONS:
 | ||
|  | 
 | ||
|  | *    **DO NOT** add solutions that are similar to any existing solutions. If you think it is **_similar but better_**, then try to merge (or replace) the existing similar solution. | ||
|  | *   Add an explanation of your solution. | ||
|  | *   Categorize the solution in one of the following categories — **Basic**, **Intermediate** and **Advanced**.  | ||
|  | *   Please add your username only if you have added any **relevant main contents**. ( **_DO NOT_** _remove any existing usernames_) | ||
|  | 
 | ||
|  | > See  <a href='http://forum.freecodecamp.com/t/algorithm-article-template/14272' target='_blank' rel='nofollow'>**`Wiki Challenge Solution Template`**</a> for reference.
 |