I tried this:
Segun Ola
1

Your array is [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. Every value in your array is non-zero, meaning they are all “truthy”.

In your first piece of code above, you’re returning n % 2 || n. This will always result in a truthy value. If n % 2 results in 0, your expression becomes 0 || n. Since || results in a truthy value if either side is truthy, and n is always truthy, your function won’t filter anything out.

It seems what you’re trying to do is return !(n % 2). That’ll get you an array with even numbers.

The second piece of code returns n % 2 && n. This will first evaluate the left side and if the left side is falsey, it will stop and return false. If the left side is truthy it will evaluate the right side, which is always truthy. This happens to do what you want, only because all of your values in the array are truthy.

With the second block, you’ll want to return just n % 2. That’ll get you an array with odd numbers.

Show your support

Clapping shows how much you appreciated Arnav Aggarwal’s story.