@@ -23,6 +23,7 @@ All of these examples are from FreeCodeCamp's [course](https://www.freecodecamp.
23
23
- [ 10. Boo who] ( #10-boo-who )
24
24
- [ 11. Title Case a Sentence] ( #11-title-case-a-sentence )
25
25
- [ 12. Slice and Splice] ( #12-slice-and-splice )
26
+ - [ 13. Falsy Bouncer] ( #13-falsy-bouncer )
26
27
27
28
## Basic Algorithm Scripting
28
29
@@ -412,3 +413,39 @@ function frankenSplice(arr1, arr2, n) {
412
413
413
414
frankenSplice ([1 , 2 , 3 ], [4 , 5 ], 1 );
414
415
```
416
+
417
+ ### 13. Falsy Bouncer
418
+
419
+ _ Difficulty: Beginner_
420
+
421
+ Remove all falsy values from an array.
422
+
423
+ Falsy values in JavaScript are false, null, 0, "", undefined, and NaN.
424
+
425
+ Hint: Try converting each value to a Boolean.
426
+
427
+ ---
428
+
429
+ #### Solution 1
430
+
431
+ ``` js
432
+ function bouncer (arr ) {
433
+ return arr .filter (Boolean );
434
+ }
435
+
436
+ bouncer ([7 , " ate" , " " , false , 9 ]);
437
+ ```
438
+
439
+ #### Solution 2
440
+
441
+ ``` js
442
+ function bouncer (arr ) {
443
+ let newArray = [];
444
+ for (let i = 0 ; i < arr .length ; i++ ) {
445
+ arr[i] && newArray .push (arr[i]);
446
+ }
447
+ return newArray;
448
+ }
449
+
450
+ bouncer ([7 , " ate" , " " , false , 9 ]);
451
+ ```
0 commit comments