Added a Basic Code Solution to "False Bouncer" challenge (#34858)
* Added a Basic Code Solution I have also provided the Code Explanation and Relevant Links. * Added 3 backticks to code Co-Authored-By: merkur0 <ondramerkun123@gmail.com> * Added 3 backticks here too Co-Authored-By: merkur0 <ondramerkun123@gmail.com> * Removed deprecated part of text Co-Authored-By: merkur0 <ondramerkun123@gmail.com> * fix: reformatted links and add closing backticks
This commit is contained in:
@ -37,12 +37,40 @@ Then we need to add a `filter()` with the falsy values function...
|
||||
|
||||
**Solution ahead!**
|
||||
|
||||
##  Advanced Code Solution:
|
||||
##  Basic Code Solution:
|
||||
|
||||
```js
|
||||
function bouncer(arr) {
|
||||
let newArray = [];
|
||||
for (var i = 0; i < arr.length; i++){
|
||||
if (arr[i])
|
||||
newArray.push(arr[i]);
|
||||
}
|
||||
return newArray;
|
||||
}
|
||||
```
|
||||
 <a href='https://repl.it/repls/WarmPlainWebportal' target='_blank' rel='nofollow'>Run Code</a>
|
||||
|
||||
### Code Explanation:
|
||||
We create a new empty array.
|
||||
We use a for cycle to iterate over all elements of the provided array (arr).
|
||||
We use the if statement to check if the current element is <a href='http://forum.freecodecamp.com/t/javascript-truthy-value/15975' target='_blank' rel='nofollow'>truthy</a> or <a href='https://guide.freecodecamp.org/javascript/falsy-values/' target='_blank' rel='nofollow'>falsy</a>.
|
||||
If the element is truthy, we push it to the new array (newArray). This result in the new array (newArray) containing only truthy elements.
|
||||
We return the new array (newArray).
|
||||
|
||||
#### Relevant Links
|
||||
|
||||
* [Boolean](https://forum.freecodecamp.com/t/javascript-boolean/14311)
|
||||
* [Truthy value](https://forum.freecodecamp.com/t/javascript-truthy-value/15975)
|
||||
* [Falsey values](https://www.freecodecamp.org/forum/t/javascript-falsy-values/14664)
|
||||
* [Array.prototype.push](https://www.freecodecamp.org/forum/t/javascript-array-prototype-push/14298)
|
||||
|
||||
##  Advanced Code Solution:
|
||||
```js
|
||||
function bouncer(arr) {
|
||||
return arr.filter(Boolean);
|
||||
}
|
||||
|
||||
```
|
||||
 <a href='https://repl.it/CLjU/32' target='_blank' rel='nofollow'>Run Code</a>
|
||||
|
||||
### Code Explanation:
|
||||
@ -59,7 +87,6 @@ The `Array.prototype.filter` method expects a function that returns a `Boolean`
|
||||
|
||||
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:
|
||||
|
||||
|
Reference in New Issue
Block a user