DEV Community

Discussion on: Handy JavaScript Array Tips

Collapse
 
s3rg1096 profile image
S3RG1096

Okay, so i have a task to find the sum of the members of even numbers in an array that are >0 . How can I form the code in js?

Collapse
 
aumayeung profile image
John Au-Yeung • Edited

You can use the filter method.

This means you write something like:

arr.filter(x => x % 2 === 0).reduce((a, b) => a + b, 0)

where arr is your array of numbers.