How to create function which return only even numbers in array?
Write a function that takes one argument, which is an array of numbers, and returns an array that contains only the numbers from the input array that are even.
you can use .filter()
with condition to check number is even or not. That will give you all even elements from an array
From Documentation:
The filter() method creates a new array with all elements that pass the test implemented
var inputs = [1, 2, 3, 4, 5, 6]
var result = inputs.filter(x => x % 2 == 0)
console.log(result);
If you want function which takes array as input parameter and return array containing all even elements then try below
const getEvenNumbers = (inputs) => inputs.filter( x => x % 2 === 0 );
console.log(getEvenNumbers([1, 2, 3, 4, 5, 6]))