+ 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]);
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
+ 2
Thank you so much.