[JS Practice] Remove odd number in an array (去掉数组中的奇数)

Title :

Remove odd number in an array (去掉数组中的奇数)

Description:

Write a small function that returns the values of an array that are not odd.

All values in the array will be integers. Return the good values in the order they are given.

要求实现一个方法,在给方法传入一个只包含数字的数组时,方法可以返回数组中以偶数组成的另外一个数组。

My solution :

function remvoe_odd( arr ){
  return arr.filter(function(e) {
    return e % 2 == 0;
  });
}

此练习的重点在于考察filter方法的使用,其对数组中所有元素应该一个callback方法,然后返回一个包含所有符合条件元素的数组。

Ref. from codewars

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s