Algorithm Falsy Bouncer

🚩 Remember to use Read-Search-Ask if you get stuck. Try to pair program πŸ‘₯ and write your own code πŸ“

🏁 Problem Explanation:

Remove all falsy values from an array.

Relevant Links

πŸ’¬ Hint: 1

Falsy is something which evaluates to FALSE. There are only six falsy values in JavaScript: undefined, null, NaN, 0, β€œβ€ (empty string), and false of course.

try to solve the problem now

πŸ’¬ Hint: 2

We need to make sure we have all the falsy values to compare, we can know it, maybe with a function with all the falsy values…

try to solve the problem now

πŸ’¬ Hint: 3

Then we need to add a filter() with the falsy values function…

try to solve the problem now

Spoiler Alert!

687474703a2f2f7777772e796f75726472756d2e636f6d2f796f75726472756d2f696d616765732f323030372f31302f31302f7265645f7761726e696e675f7369676e5f322e676966.gif

Solution ahead!

🚨 Advanced Code Solution:

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

πŸš€ Run Code

Code Explanation:

The Array.prototype.filter method expects a function that returns a Boolean value which takes a single argument and returns true for truthy value or false for falsy value. Hence we pass the built-in Boolean function.

Relevant Links

πŸ† Credits:

If you found this page useful, you can give thanks by copying and pasting this on the main chat:

Thanks @renelis @abhisekp @Rafase282 for your help with Algorithm: Falsy Bouncer

πŸ“‹ NOTES FOR CONTRIBUTIONS:

  • ⚠ DO NOT add solutions that are similar to any existing solutions. If you think it is similar but better, then try to merge (or replace) the existing similar solution.
  • Add an explanation of your solution.
  • Categorize the solution in one of the following categories β€” Basic, Intermediate and Advanced. πŸš₯
  • Please add your username only if you have added any relevant main contents. (⚠ DO NOT remove any existing usernames)

See πŸ‘‰ Wiki Challenge Solution Template for reference.