I'm trying to write a function that will take an array of numbers and return an array with those numbers doubled. For example 1, 2, 3
and return should be 2, 4, 6
.
This is what I have so far:
const numbers = [1, 2, 3];
function doubleNumbers(numbers) {
return numbers = 1 * 2 + ", " + 2 * 2 + ", " + 3 * 2;
}
console.log(doubleNumbers(numbers));
This is putting out the correct answer in the console, although I have a feeling something is off.
When I change the const numbers=[1, 2, 3]
to any other number, I'm still getting 2,4,6. Which leads me to believe function doubleNumbers
isn't pulling the numbers from the array, only multiplying the numbers I have in return numbers
.
You can use Array#map
.
const numbers = [1, 2, 3];
function doubleNumbers(numbers) {
return numbers.map(x => x * 2);
}
console.log(doubleNumbers(numbers));