I am not getting the first two test cases right. Idk why?

Tell us what’s happening:

  **Your code so far**

function isPositive( val ) { 
  return(val === false || val === null || val === 0 || val === "" || val === undefined || isNaN(val));
} 

function bouncer(arr) {
let temp = [];
for(let i=0;i<arr.length;i++){
  if(!isPositive(arr[i])) temp.push(arr[i]);
}
return temp;
}

bouncer([null, NaN, 1, 2, undefined]);
  **Your browser information:**

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

Challenge: Falsy Bouncer

Link to the challenge:

Because 'ate', 'a', 'b', 'c' pass the isNaN(value) and returns true. Therefore is not returning undefined. Your code pushes all the undefined from isPositive function. So all the strings doesn’t get pushed

Notice that isNaN means is Not a Number, those are strings. String is Not a Number

Oh yeah!! Totally forgot about that. Thank you @danzel-py . I will make the changes

@danzel-py when i replace that with val === NaN, the last two test cases don’t work. Could you please help me

Hi I think your code is over-creative :laughing:. It might be simpler if you just go ahead and return true values.

But we still can use your code. Though it’s a bit theoretical. You might want to read about NaN - JavaScript.

Notice that

NaN === NaN; // false

Therefore instead of isNaN(value) we can use

val !== val

Because

NaN !== NaN returns true

Edit: This is in JavaScript, it may be different in other language. Sooo in the end i still suggest for writing a simpler code. We can easily bounce the falsy value by ignoring every false value and pushing every true value

wow. I got it right. Yes i’ll read that article. Thank you so much @danzel-py . And one more thing how do i simplify it?

function bouncer(arr) {
let temp = [];
for(let i=0;i<arr.length;i++){
  if((arr[i])) temp.push(arr[i]);
}
return temp;
}

bouncer([null, NaN, 1, 2, undefined]);

I think you’ll understand

is if((arr[i])) similar to if(true) over here ?

Yeah sorry for the bad writing example, actually

if(arr[i])

will execute whenever arr[i] is true

oh got it now!!!. Thank you @danzel-py

You might want to read this about JavaScript Boolean.

Everything With a “Value” is True

Will go through it thanks @danzel-py

1 Like

Glad i can help. Actually i also learnt a lot lol.

if(arr[i])

is actually the short version of

if(Boolean(arr[i]))

Happy coding!

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