2018-10-04 14:37:37 +01:00
|
|
|
---
|
|
|
|
id: adf08ec01beb4f99fc7a68f2
|
|
|
|
title: Falsy Bouncer
|
|
|
|
challengeType: 5
|
2019-07-31 11:32:23 -07:00
|
|
|
forumTopicId: 16014
|
2018-10-04 14:37:37 +01:00
|
|
|
---
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
# --description--
|
|
|
|
|
2018-10-04 14:37:37 +01:00
|
|
|
Remove all falsy values from an array.
|
2020-11-27 19:02:05 +01:00
|
|
|
|
|
|
|
Falsy values in JavaScript are `false`, `null`, `0`, `""`, `undefined`, and `NaN`.
|
|
|
|
|
2018-10-04 14:37:37 +01:00
|
|
|
Hint: Try converting each value to a Boolean.
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
# --hints--
|
2018-10-04 14:37:37 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
`bouncer([7, "ate", "", false, 9])` should return `[7, "ate", 9]`.
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert.deepEqual(bouncer([7, 'ate', '', false, 9]), [7, 'ate', 9]);
|
|
|
|
```
|
2018-10-04 14:37:37 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
`bouncer(["a", "b", "c"])` should return `["a", "b", "c"]`.
|
2018-10-04 14:37:37 +01:00
|
|
|
|
|
|
|
```js
|
2020-11-27 19:02:05 +01:00
|
|
|
assert.deepEqual(bouncer(['a', 'b', 'c']), ['a', 'b', 'c']);
|
|
|
|
```
|
2018-10-04 14:37:37 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
`bouncer([false, null, 0, NaN, undefined, ""])` should return `[]`.
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert.deepEqual(bouncer([false, null, 0, NaN, undefined, '']), []);
|
2018-10-04 14:37:37 +01:00
|
|
|
```
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
`bouncer([null, NaN, 1, 2, undefined])` should return `[1, 2]`.
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert.deepEqual(bouncer([null, NaN, 1, 2, undefined]), [1, 2]);
|
|
|
|
```
|
2018-10-04 14:37:37 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
# --seed--
|
2018-10-04 14:37:37 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
## --seed-contents--
|
2018-10-04 14:37:37 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
```js
|
|
|
|
function bouncer(arr) {
|
|
|
|
return arr;
|
|
|
|
}
|
2018-10-04 14:37:37 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
bouncer([7, "ate", "", false, 9]);
|
|
|
|
```
|
2018-10-04 14:37:37 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
# --solutions--
|
2018-10-04 14:37:37 +01:00
|
|
|
|
|
|
|
```js
|
|
|
|
function bouncer(arr) {
|
|
|
|
return arr.filter(e => e);
|
|
|
|
}
|
|
|
|
|
|
|
|
bouncer([7, "ate", "", false, 9]);
|
|
|
|
```
|