DEV Community

Cover image for Filter out the geese (Codewars - 8kyu)
Julia Furst Morgado
Julia Furst Morgado

Posted on

Filter out the geese (Codewars - 8kyu)

Here's a link to the Codewars Kata: https://www.codewars.com/kata/57ee4a67108d3fd9eb0000e7/javascript

The goal of this exercise is to get back an array without some specific elements in it (elements that are specified in the geese array).

Start the function

Here we'll create a function called gooseFilter that accepts a parameter named birds of type Array.

function gooseFilter(birds) {
//function body
}
Enter fullscreen mode Exit fullscreen mode

Return the string with methods applied to it

return str
Enter fullscreen mode Exit fullscreen mode

We start with a return statement because we already want the result of the function right off the bat.

Filter the array

.filter()
Enter fullscreen mode Exit fullscreen mode

We use the filter method to get a subset of the original array (birds) based on specific criteria. The filter method tests each element of the array, so in our case here, we want to pull out ONLY the items that are not included in the geese array.

.filter(item => !geese.includes(item))
Enter fullscreen mode Exit fullscreen mode

Summary

The final function looks like this:

function gooseFilter(birds){
let geese =  ["African", "Roman Tufted", "Toulouse", "Pilgrim", "Steinbacher"]
return birds.filter(item=> !geese.includes(item))
}
Enter fullscreen mode Exit fullscreen mode

I hope it helps you!

If you liked this article please follow me on Dev.to for my latest articles. I'm tweeting my journey on Twitter daily, this way to my LinkedIn, and this is my Youtube channel :)

I share my knowledge on,

  • šŸŒ Web Development
  • āœļø Content Creation
  • šŸ’¼ Career Development
  • šŸ¦¾Personal Growth
  • And more!

Top comments (0)