0

I don't understand why this piece of code works can someone explain to me?

If I delete this piece of the conditional && arr[i] the value of arr[5] don't assume as a falsy value, but if I write that piece of code already assumes arr[5] like a falsy value.

You can see the value of arr[5] on the end of the function.

function bouncer(arr) {
  let word = []
  for (let i = 0; i < arr.length; i++)
    if (typeof arr[i] !== Boolean && arr[i]) {
      word.push(arr[i])
    }
  return word;
}

console.log(bouncer([false, null, 0, NaN, undefined, ""]));
Andreas
  • 21,535
  • 7
  • 47
  • 56
  • 1
    With `i < arr.length` you specify to go until position 4 not 5.. if you want to go until pos 5, you need to say `i <= arr.length`.. below or equal with pos 5 – Mara Black Feb 21 '22 at 16:44
  • 1
    In the code snippet you shared, I can't find a opening and closing brace for for loop – Shiju Nambiar Feb 21 '22 at 16:44
  • 1
    @MaraBlack Arrays are zero-based. The last element is at `arr.length - 1`. If you change the condition to `<= arr.length` you're out of bounds because `arr[arr.length]` doesn't exist – Andreas Feb 21 '22 at 16:46
  • Can you please clarify the problem? Your script works exactly as expected. None of the values is truthy, hence with the `&& arr[i]` check the result is empty (check the length of the result). If you remove that check the result will only miss the first element because that's the only actual boolean. – Andreas Feb 21 '22 at 16:52
  • @ShijuNambiar They are "optional". If you don't use them, then the next statement will be part of the loop, but nothing else. The function is actually: `let word = []; for ( ... ) { if ( ... ) { ... } } return word;` – Andreas Feb 21 '22 at 16:55

2 Answers2

0

Try this code if you want to check for falsy values

Let me tell you that falsy values don't require to be boolean!

For Example, 0 is a falsy value however typeof(0) is number ..

The reason why your code returns an empty array is the if condition, you're checking if typeof(arr[i]) !== Boolean && arr[i]

this condition will always be false since typeof returns a string and Boolean is considered as an Object/ Constructor (which isn't a string)

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

console.log(bouncer([false, null, 0, NaN, undefined, ""]));
Salwa A. Soliman
  • 695
  • 1
  • 3
  • 13
-1

maybe You can do it simplier :-)

let arr = [false, null, 0, NaN, undefined, "", 111, "aaa"];

let x = arr.filter(i => (typeof i !== Boolean && i))

enter image description here

Marek Lisiecki
  • 498
  • 6
  • 10
  • What have you changed? Why? And why does this solve OPs problem (which is understanding the behavior of the script)? – Andreas Feb 21 '22 at 17:22
  • `typeof` returns a string. You compare a string with a type (`typeof i !== Boolean`). Why should this work (hint: it doesn't)? – Andreas Feb 21 '22 at 17:24
  • If I see code like this I alwayt try to suggest use of simplier solution just for educational purpose – Marek Lisiecki Feb 21 '22 at 17:24
  • @Andreas numeric and string are for testcase only -) – Marek Lisiecki Feb 21 '22 at 17:25
  • `typeof i` with `false` -> `"boolean"`. How is `"boolean" !== Boolean` going to do anything useful? – Andreas Feb 21 '22 at 17:26
  • @Andreas If You don't understand, i just do some refactor to show simplier solution of source code in this question and i think author and community should have some benefit from it – Marek Lisiecki Feb 21 '22 at 17:33
  • Your answer doesn't work. _"i think author and community should have some benefit from it"_ - What's the benefit of non-working code? – Andreas Feb 21 '22 at 17:36
  • it checking non falsy values result is `[ 111, 'aaa' ]` this is only example of course, tested on NodeJs, and in chrome console. – Marek Lisiecki Feb 21 '22 at 17:40
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/242238/discussion-between-marek-lisiecki-and-andreas). – Marek Lisiecki Feb 21 '22 at 17:44