JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – array filter method can filter odd value

JavaScript Quiz – array filter method can filter odd value

Array filter method –

JavaScript array filter method (Array.forEach()) is used to filter a array element based on condition and create new array with filtered element.

It’s syntax looks like –

let newArray = arr.filter(callback(element[, index, [array]])[, thisArg])

For more detail on JavaScript Array Filter Method click here.

JavaScript Question

In this javascript quiz code, you need to find out does array filter method can filter odd value from a given number element array.

First we declare a variable array using let keyword and assign an javascript array to it. Which contain only number element.

let array = [1, 2, 3, 4, 5];

Now, create another variable newArray using let and assign an array return from the array filter method.

let newArray = array.filter((el) => { … }) ;

But we also pass a condition to filter method which decide which element should be remove from array variable.

((el) => el%2 ) ;

Finally, we are checking the console log for newArray variable. Any idea what is the output.

Answer – [1, 3, 5]

Click To Run Code

Explanation

Now, you know the answer is [1, 2, 3]. But how it’s working. Let me explain

So, as we can see that we pass a condition to filter method but what it return we do not know.

Let’s deep dive into it. we use el%2 but what is %. So, it’s called Modulus (Remainder). The modulus operator (%) returns the division remainder.

As we are using with value 2. So every element get pass through it and return a remainder based on el value.

But as we can see in array, number element 2 & 4 is the only one give remainder 0.

And 0 is a falsy value which returns false to filter condition due to which they are not included in newArray.

That way, we can remove even value from array and filter out only odd value.

At last, That’s why we get an answer “[1, 3, 5]“.

I hope you understand the concept and logic behind it.

But wait, i have something more for you –

Check out our other javascript quiz – 

  1. JavaScript Quiz – can isFrozen method really check freeze object
  2. JS Quiz – new set object can return the same size
  3. JavaScript Quiz – can break statement work on array forEach method
  4. JS Quiz – variable can be key and value in object

Conclusion

To participate in our javascript quiz or challenges, tutorial, tips & tricks make sure to join our jsstartup newsletter. So, you can able to participate in our daily challenges & learn the javascript concept.

And last but not the least, don’t forget to like, comment and share. It gives us a morale boost to remain to continue. Also, join our Facebook Page

If you have any questions, please feel free to ask me in the comment section and also let me know if you have any suggestions. As suggestions are always welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top
0 Shares
Share via
Copy link