72 lines
		
	
	
		
			4.6 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
		
		
			
		
	
	
			72 lines
		
	
	
		
			4.6 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
|   | --- | ||
|  | title: Falsy Bouncer | ||
|  | --- | ||
|  |  | ||
|  | 
 | ||
|  |  Remember to use <a>**`Read-Search-Ask`**</a> if you get stuck. Try to pair program  and write your own code  | ||
|  | 
 | ||
|  | ###  Problem Explanation:
 | ||
|  | 
 | ||
|  | Remove all <a href='https://guide.freecodecamp.org/javascript/falsy-values/' target='_blank' rel='nofollow'>falsy</a> values from an array. | ||
|  | 
 | ||
|  | #### Relevant Links
 | ||
|  | 
 | ||
|  | *   <a href='https://guide.freecodecamp.org/javascript/falsy-values/' target='_blank' rel='nofollow'>Falsy Values</a> | ||
|  | 
 | ||
|  | ##  Hint: 1
 | ||
|  | 
 | ||
|  | Falsy is something which evaluates to FALSE. There are only six falsy values in JavaScript: undefined, null, NaN, 0, "" (empty string), and false of course. | ||
|  | 
 | ||
|  | > _try to solve the problem now_
 | ||
|  | 
 | ||
|  | ##  Hint: 2
 | ||
|  | 
 | ||
|  | We need to make sure we have all the falsy values to compare, we can know it, maybe with a function with all the falsy values... | ||
|  | 
 | ||
|  | > _try to solve the problem now_
 | ||
|  | 
 | ||
|  | ##  Hint: 3
 | ||
|  | 
 | ||
|  | Then we need to add a `filter()` with the falsy values function... | ||
|  | 
 | ||
|  | > _try to solve the problem now_
 | ||
|  | 
 | ||
|  | ## Spoiler Alert!
 | ||
|  | 
 | ||
|  |  | ||
|  | 
 | ||
|  | **Solution ahead!** | ||
|  | 
 | ||
|  | ##  Advanced Code Solution:
 | ||
|  | 
 | ||
|  |     function bouncer(arr) { | ||
|  |       return arr.filter(Boolean); | ||
|  |     } | ||
|  | 
 | ||
|  |  <a href='https://repl.it/CLjU/32' target='_blank' rel='nofollow'>Run Code</a> | ||
|  | 
 | ||
|  | ### Code Explanation:
 | ||
|  | 
 | ||
|  | The `Array.prototype.filter` method expects a function that returns a `Boolean` value which takes a single argument and returns `true` for <a href='http://forum.freecodecamp.com/t/javascript-truthy-value/15975' target='_blank' rel='nofollow'>truthy</a> value or `false` for <a href='https://guide.freecodecamp.org/javascript/falsy-values/' target='_blank' rel='nofollow'>falsy</a> value. Hence we pass the built-in `Boolean` function. | ||
|  | 
 | ||
|  | #### Relevant Links
 | ||
|  | 
 | ||
|  | *   <a href='http://forum.freecodecamp.com/t/javascript-boolean/14311' target='_blank' rel='nofollow'>Boolean</a> | ||
|  | *   <a href='http://forum.freecodecamp.com/t/javascript-truthy-value/15975' target='_blank' rel='nofollow'>Truthy</a> | ||
|  | *   <a href='http://forum.freecodecamp.com/t/javascript-array-prototype-filter/14289' target='_blank' rel='nofollow'>Array.prototype.filter()</a> | ||
|  | 
 | ||
|  | ##  Credits:
 | ||
|  | 
 | ||
|  | If you found this page useful, you can give thanks by copying and pasting this on the main chat: | ||
|  | 
 | ||
|  | **`Thanks @renelis @abhisekp @Rafase282 for your help with Algorithm: Falsy Bouncer`** | ||
|  | 
 | ||
|  | ##  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.
 |