Basic Algorithm Scripting - Falsy Bouncer

Tell us what’s happening:

why wont this code work?

Your code so far

function bouncer(arr) {let ne=[]; for(let i=0;i<arr.length;i++)  if (arr[i] !== false && arr[i] !== null && arr[i] !== 0 && arr[i] !== "" && arr[i] !== undefined && !isNaN(arr[i])) {
      ne.push(arr[i]);
    } return ne
  } 

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

Your browser information:

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

Challenge Information:

Basic Algorithm Scripting - Falsy Bouncer

Instead of manually comparing and filtering out the elements you can use the filter function in javascript to remove all the falsy values from the array.

The syntax of the function is as follows

arr.filter(Boolean)

Boolean is a built-in JavaScript function that coerces its argument to a boolean value. It returns false for falsy values (undefined , null , 0 , NaN , "" , false ), and true for truthy values.
Any element that is falsy (according to the Boolean conversion rules) will be excluded from the resulting array because Boolean(falsyValue) returns false .

Hope this helps!

1 Like

I see that thank you so much! but I was trying to pass the lesson with various methods. I was wondering what went wrong here since I dont want to have any concepts left foggy.

function bouncer(arr) {
  let ne = [];
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] !== false && arr[i] !== null && arr[i] !== 0 && arr[i] !== "" && arr[i] !== undefined && !isNaN(arr[i])) {
      ne.push(arr[i]);
    }
  }
  return ne;
}

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

The way you wrote the code obfuscates what’s happening.

Note - ne is a pretty cryptic variable name.

The test cases with strings aren’t working right. Your problem is with the isNaN function. Double check what it does with non-numeric inputs

Note - all three things that the AI said are wrong. This is the problem with asking large language models.

1 Like

Please don’t write out the answer for people

1 Like

@JeremyLT Yes I understand, but I was just trying to write the syntax for the function. I will definitely take care next time!

But you didn’t show the syntax for using Boolean(), only how to use Boolean and filter to solve the Challenge.

console.log(Boolean("cat"));

shows what Boolean() does without giving the whole answer.

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