# Double even numbers

The doubleEvenNumbers returns only the even numbers in the array `myNumbers` and doubles them. the code below works perfectly but i have to rewrite it using map and filters function.

``````function doubleEvenNumbers(numbers) {
const newNumbers = [];
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] % 2 === 0) {
newNumbers.push(numbers[i] * 2);
}
}
return newNumbers;
}

const myNumbers = [1, 2, 3, 4];
console.log(doubleEvenNumbers(myNumbers)); // Logs "[4, 8]" to the console
``````

Here is the code i am trying to write using map and filter, this code should return even numbers and double them, hence it is supposed to be [4,8] but my problem is that it returns [1,3] please help… i am stuck.

``````const doubleEvenNumbers = (numbers) => {
let newNums = numbers.map(x => x*2);
newNums = numbers.filter(x => x%2);
return newNums;
}
const myNumbers = [1,2,3,4];
console.log(doubleEvenNumbers(myNumbers));
``````

Hello Dkpro.

You I would suggest going over what the `filter` method does. Otherwise, you are close to achieving your required outcome.

``````const doubleEvenNumbers = (numbers) => {
let newNums = numbers.filter(x => x%2 == 0); //Filter first
newNums = newNums.map(x => x*2);
return newNums;
}
const myNumbers = [1,2,3,4];
console.log(doubleEvenNumbers(myNumbers));
``````

Happy coding.

1 Like

if you double numbers first, all of them will be divisible by 2 - maybe not the right course of action

2 Likes

Hello @Sky020 Thanks a lot. it worked.