+ 2

How it works this js fuction??

Hello guys, can some one explain to me how it works this code?? var c = new Array (1,2,3,4); var b = c.filter(function(a){return(a%2);}; alert(b[0] + b[1]); Output is 4 What value does 'a' take, where does it take it from?

8th Aug 2019, 3:59 PM
Yoel
Yoel - avatar
3 Antworten
+ 11
c.filter iterates all value of array 'c' and in each iteration the value of 'a' is one value of the array 'c'. So when filter iterates it divides each value by 2 then returns the remainder and converts it into boolean and if the value is true then the value is added to new array 'b' therefore the value of b is [1,3] because only 1%2 and 3%2 returns 1 that is true. For more information about Array.prototype.filter() you can visit: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter Also there is syntax error in the code because bracket of c.filter is not closed.
8th Aug 2019, 4:09 PM
VEDANG
VEDANG - avatar
+ 4
VEDANG and ~ swim ~ ,thank you very much, I get it
8th Aug 2019, 5:24 PM
Yoel
Yoel - avatar