Skip to content

Commit 2f1681b

Browse files
committed
falsy bouncer ⚠️
1 parent 93e8d09 commit 2f1681b

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ All of these examples are from FreeCodeCamp's [course](https://www.freecodecamp.
2323
- [10. Boo who](#10-boo-who)
2424
- [11. Title Case a Sentence](#11-title-case-a-sentence)
2525
- [12. Slice and Splice](#12-slice-and-splice)
26+
- [13. Falsy Bouncer](#13-falsy-bouncer)
2627

2728
## Basic Algorithm Scripting
2829

@@ -412,3 +413,39 @@ function frankenSplice(arr1, arr2, n) {
412413

413414
frankenSplice([1, 2, 3], [4, 5], 1);
414415
```
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

Comments
 (0)