+ 2

Explain please (Javascript):

var c = new array(1,2,3,4); var b = c.filter(function(a){ return(a%2-1); }); alert(b[0]);

15th Dec 2019, 1:53 PM
A.H. Majumder 🇧🇩
A.H. Majumder 🇧🇩 - avatar
2 Respuestas
+ 4
the filter method creates a new array with value that returns "true" in the above code. it creates an array of even numbers only. when (a%2-1) is 0 it skips to next when it's -1 it adds that element to the new array (b). i.e: when a = 1 ; (1%2-1) = 0 //false when a = 2 ; (2%2-1)= -1 //true since 2 is the first even number it is the first element in b[] so b[0] = 2, b[1] = 4
15th Dec 2019, 3:25 PM
Bahhaⵣ
Bahhaⵣ - avatar
+ 2
Thank you so much.
16th Dec 2019, 1:38 AM
A.H. Majumder 🇧🇩
A.H. Majumder 🇧🇩 - avatar