Falsy Bouncer - it works but i dont understand how

Falsy Bouncer - it works but i dont understand how
0.0 0

#1

So I ctrl + etr’d this code to check what else i needed to filter out and it worked for the whole lot. but I don’t understand how?

function bouncer(arr) {
// Don’t show a false ID to this bouncer.

function checkFalsy (value) {
if (typeof(value) === ‘number’ || true ) {
return value;
}
}

var bouncedArr = arr.filter(checkFalsy);

return bouncedArr;
}

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

It should filter out strings at the moment?


#2

Yes. Array.filter uses a callback and hands each element of the array to that callback. It will filter anything that doesn’t return true.

When you pass checkFalsy into arr.filter, you are using checkFalsy as a callback.

So what happens is checkFalsy is seeing 7 and saying is 7 typeOf number? Yes. True. So 7 doesn’t get filtered out.

When it checks ‘ate’, well that isn’t typeOf number, since its a string. So that returns false, and arr.filter drops it.

See? So if you want to play around, try modifying checkFalsy. Change the if statement to

if(value === ‘ate’){return value + ‘my cat’}

When you feed it that same array, it should only return ‘ate my cat.’

Hope that helps!


#3

Thanks, my problem is that code spits out [7, “ate”, 0] - I dont think it should be spitting out “ate”?


#4

It is very simple.

function bouncer(arr) {

  function checkFalsy (value) { // 'value' is what is being tested
    // check if the value is 1. a number, OR 2. a boolean
    // 'true' variables are either a boolean 'true', or a number.
    if (typeof(value) === 'number' || true ) {      
      return value;   //if the value passes test return it.
    }  
  }

  // filter takes every element in this array,
  // and tests it in the above function.
  // If the element in array passes the test above,
  // it is returned like previously stated.
  // only 'true values which pass the test are returned'
  // you now have an array with only 'true' values.
  return arr.filter(checkFalsy);
}

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

#5

Any string with a value in it is true. “” is false because its length is 0.

Falsy values in JavaScript are false, null, 0, “”, undefined, and NaN.


#6

@IssacAbrahamson has it right. I neglected to notice the ’ || true’ in the problem.

Here’s code that would filter anything that wasn’t a number (if that’s what you wanted, I don’t think that’s the goal of the challenge):

function myFunc(){

function checkFalsy (value) {
if (typeof(value) === 'number' ) {
return value;
}
}

var bouncer =[7, "ate", "", false, 9];

var bouncedArr = bouncer.filter(checkFalsy);

return bouncedArr;
}

myFunc();

#7

There is an even faster way. Since you are checking if values are true, all you need is one if statement. Extremely simple.

function checkFalse(value) {
    if (value) {
      return value;
    }
}

function bouncer(arr) {
  return arr.filter(checkFalse);
}

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

or with a callback:

function bouncer(arr) {
  return arr.filter(function(value) {
    if (value) {
      return value;
    }
  });
}

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

#8

Thanks that makes sense!


#9

Is there a policy around here about posting solutions to the coding problems on FCC? Feels like there should be spoiler alert tags or something.


#10

I normally would not post a solution, but seeing how the OP had already posted his working one, I don’t see any reason for not posting a piece of code that was easier to understand which is what he was looking for.


#12

Yeah, that’s why I was thinking the spoiler alert type tag, so people could discuss but people still working on the problem didn’t get their fun wrecked. Not a big deal.


#14

Yeah. all the solutions to challenges and algorithms are literally on the forum wiki to read.


#15

As the condition in the if is always true, the code

  function checkFalsy (value) {
    if (typeof(value) === 'number' || true ) {
      return value;
    }
  }

is same as

  function checkFalsy (value) {
    return value;
  }

(the if is just there for distraction)

filter then calls the callback for every element, and whenever it receives a truthy value, it will include that element in the returned array.

Please note that the returned array contains the original array’s elements, not the callback results, these are only evaluated on whether they are truthy or falsy!


#16

Ah right, I understand… thanks!


#17

I kind of stumbled on the solution to this problem on my own. I also didn’t understand how it worked, which lead me to this forum thread. I ended up with this code:

function bouncer(arr) {

function isNotTrue(value) {
return value;
}
return arr.filter(isNotTrue);
}

bouncer([7, “ate”, 0, “”, false, 9, 33, NaN, null, undefined]);

But then after reading this thread I changed it to the code below and it also allowed me to pass the tests. My question is; Which solution is the better way of doing it? And why?

function bouncer(arr) {
return arr.filter(isNotTrue);
}

function isNotTrue(value) {
return value;
}

bouncer([7, “ate”, 0, “”, false, 9, 33, NaN, null, undefined]);


#18

In the function:

function checkFalse(value) {
if (value) {
return value;
}
}

what does
if (value)
actually do?


#19

I just completed this challenge and was curious to see other solutions. I actually used the Boolean object as instructed and didn’t work my way around it as initially tempted. It took a good half hour to wrap my head around the concept of the Boolean object.

My solution
function bouncer(arr) {
   function check(x) {
      return Boolean(x);
   }

   var newArr = arr.filter(check);
   return newArr;
}

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

#20

This is exactly what I did. I also felt like the solution needed to include the Boolean object since it was referenced. (though I don’t always follow the hints and I’ve been known to solve the challenge in a different way). It also took me quite a while to get my head around the code. I kept re-reading the MDN pages on Boolean and .Filter and each time I felt like I understood one more tiny amount.

I stared at my code for a while trying to figure out why it wasn’t working and finally I put the “return” in front of the Boolean(arr) statement in my function and presto, it passed. I was thinking that I didn’t need to “return” a boolean statement, and that it did that automatically. I guess not.

Here’s my code, pretty much identical to Soupedenuit above:

function bouncer(arr) {
var newArr = [];

  function checkWord(arr) {
   return Boolean(arr);
} 
  
  newArr = arr.filter(checkWord); 
  
return newArr;
}

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