NEED Help Falsy Bouncer

Tell us what’s happening:

Your code so far


function bouncer(arr) {
  // Don't show a false ID to this bouncer.
    console.log(arr)
  for(let i=0;i<arr.length;i++){
    if (arr[i] == false || arr[i] == null || arr[i] == 0 || arr[i] == "" || arr[i] == NaN || arr[i] == undefined){
      arr.splice(i,1);
    }
  }
  console.log(arr)
  return arr;
}

bouncer([7, "ate", "", false, 9]);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.75 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/falsy-bouncer

instead of all those or statements
try this
if (!arr[i]) {

what it does is it gives true if arr[i] is not a truthy
(so all those values you are trying to explicitly check for are not truthy)

1 Like

but what is wrong with the code i wrote above

Two things:

In JavaScript, nothing is equal to NaN. NaN is not even equal to itself. You can use Number.isNaN(var) to check if a variable is NaN.

Each time you splice an array, it gets shorter, but you haven’t done anything to adjust i accordingly.

2 Likes

When you are so explicit with the falsy values, you miss the point of the challenge. which is to show that things that have certain values will evaluate to false in a boolean context.

can you elaborate please

I’ll give you an example

var aNumber = 0;
if(aNumber) {
  return "truthy";
} else {
  return "falsy";
}
//output will give you "falsy"

just to add a bit more to what @zapcannon99 is saying,

true and false are both boolean values but
a ‘falsy’ is value that is not Boolean but still evaluates to false (in boolean context), eg. 0
a ‘truthy’ is a value is not Boolean but still evaluates to true (in boolean context), eg. 1

  • Removing all falsy values from an array = return all truthy values.
  • The filter() method creates a new array with all elements that pass the test implemented by the provided function. (MDN definition)
  • If .filter() method is properly implemented, you’ll return only those values which return true in the Boolean context – thus solving the challenge in one line
1 Like

function bouncer(arr) {
// Don’t show a false ID to this bouncer.
filteredArr = [false, null, 0, “”, undefined, NaN].filter(bouncer, !arr, !arr.length);

return filteredArr;
}

bouncer([7, “ate”, “”, false, 9]);

I got Maximum call size stack exceeded. Really need some assistance. Thank you

Go ahead and check out the docs for filter again, your syntax is not correct.

The method should be called on the array you want to filter, in this case the arr argument.

Next you need to pass a callback function that returns true on values you want to keep, and false on values you want to remove.


Here’s an example:

function extractEvenNumbers(numbers) {
  return numbers.filter(number => number % 2 === 0) // returns true if number is even
}

const someNumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const someEvenNumbers = extractEvenNumbers(someNumbers);
// someNumbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
// someEvenNumbers: [2, 4, 6, 8, 10]