How to avoid NaN in array? - Falsy Bouncer Challenge

The link to the falsy bouncer challenge

How to avoid NaN values in JavaScript arrays?
that’s the only thing lacking to pass this challenge:

function bouncer(arr) {
 const newArr = arr.filter(bouncer);

function bouncer(truthy) {
return truthy !== false & truthy !== "" & truthy !== null & truthy !== 0 & truthy !== "" & truthy !== undefined;
  return newArr;
console.log(bouncer([7, "ate", "", false, 9]));
1 Like

I think you’re overthinking things a little. For instance the Javascript Boolean() method could be used with filter to remove falsy values from an array. Or, as hinted by the link, there’s an even simpler approach. You can pass the challenge simply by changing the given return statement (which was ‘return arr’).

Hi @QueenDaenerys

Remember that you can filter an array using any condition and variables are already or truthy or falsy, so… :wink: filter

Since they want you return a new array with different values, map even looks better for me. map

As @igorgetmeabrain suggested you, you could map your array and return an array of falsy values converted to booleans.

Don’t be to hard with yourself, in my experience is better read the challenge, try to understand it ( this is key), read about some methods and try something. Do this for 5-10 min (+ reading) and go to another task (if you are rested, you could jump to next algorithm as well ) or back later or even tomorrow to the challenge. I found much easier to solve challenges giving time to my brain to think about it on background. As newies we focus too much at first solution it comes out and we should think deeper in the question first.

The other greates reference for JavaScript is page, fully recommended.

I hope this help, keep working and reward yourself for trying too. :muscle: :pretzel: Happy coding!! :blush:

1 Like

Also, I don’t thing & does what you think it does.

@kevinSmith why not?
in console.log the result is close to the one I need to pass this challenge

What is &? What is &&?

In this case it may work by ¨accident¨, but it´s not the right tool.

1 Like

I can probably count on one hand the number of times I really meant & instead of &&

& is a bitwise operator and is rarely used in javascript.
&& is the logical operator which is what you are chasing after, as you are differentiating between truthy and falsy elements.

Actually, my linter throws a fit if I use one. It’s so rarely used in JS.

1 Like

As said, you do not want to check for every falsy value explicitly. But if you did want to check for NaN you can use Number.isNaN()

map is not the correct choice here. It is used to transform the values, not change the array length.

bouncer([false, null, 0, NaN, undefined, ""]) should return [] .

You are right, I have read wrong the challenge. My first option it seems the correct choice, then.
Thank you

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.