Javascript Coding Challenge for Beginners Q12

Transcript

English (Auto-generated)

Hello everyone Welcome back to my javascript coding challenge for beginners. For today. We'll be using the filter method and have you guys seen before? We have done the math method. But what it does is it returns every single element inside the array. Even if the output is undefined as we have seen before in question 11. So today we're gonna use filter to get the right amount of elements that we want in our new array. Sorry, I'm a bit sick. So hopefully the voice doesn't crack out. So back to our question today, we want to use filter to return the elements. What's a tight cat? So here we have two cats, a skunk and a dog. So looking at this output, we definitely want a new array with Tigger and Winnie inside. So pause a video and come up with your own solution. And when you're ready we can tackle the solution together. So now that you're ready, let's get to the problem. So we want to put it in the video variable car results, our dog filter. I don't know why there was a zero inside. I'm sorry. Um Oh whoops. There's that Our bracket by accident. So here we have our dot filter and as usual are dot filter takes in the three parameters. The first one is elements you can call it anything you want. But basically the current iteration of our race. Our first iteration will be first integral last name kim and type cats. The second parameters. Index basically the index of the array and the third optional parameter is array. But in this example we won't be using index or array either. So we can safely remove them. And filter checks for conditions. So if the condition is true, we return the element. If not, then we don't return the element. So for example, in this case we want the typecast. So if element thought, type is equal to cat, we want to return the elements. So let's check if it returns what we want. The terminal also notes a love. And okay. No 12 dot Js. So as you can see, it does return our two elements which is um first name, last name, kim and Kathy and Winnie Cannon, Captain. However, as you guys can see in the map, there is a difference because filter filters out only what we want and it doesn't give us all of the elements inside the inside the area. So for filter, if I can give you four elements, it will only return the ones that are chosen. However, if you guys remember a map, map returns every single element given. So if you give them five elements, map will return back five elements to use, even if it does not meet the conditions. So that is something to remember to keep in mind. And filter also doesn't mutate the original array. Filter returns a new array to you. So thank you guys for watching when you're ready, we can work on the next solution together
6 Views 0 Likes 0 Comments

Use filter() to solve the coding challenge

Comment
Leave a comment (supports markdown format)