Falsy Bouncer, Boolean() function and falsy values doubts

Hi there! I’m so lost with 4 of the Basic Algorithm Scripting. This one seems to be a bit too technical so maybe I’m misunderstanding the Boolean() function or the falsy values or both. I basically though that the if condition will delete the array’s entry if it’s false. (That did work with just the array passed [“a”, “b”, “c”] but not with the others and I’m not getting why. I saw the two solutions and I understood them but they didn’t cleared why my algorithm is not working. Thanks in advance!

Your code so far


function bouncer(arr) {

for (let i = 0; i<arr.length; i++){
  if (Boolean(arr[i]) === false) {
      arr.splice(i, 1);
  }
}
return arr;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0.

Challenge: Falsy Bouncer

Link to the challenge:

splice will change the array on which it is used, having the consequence that some elements will not be checked

OMG I totally overlooked that index-thingy when iterating and eliminating stuff. This worked so well changing the statement to === true and creating a new array to push things there. Thanks a lot!!!

awesome!
an other thing, you don’t need to use Boolean to use the falsyness or truthiness of a value

if ("a") {
   // this will execute
}

if (0) {
   // this will not execute
}
1 Like