Ad
Arrays
Fundamentals

Multiply Every Element

Create a function called multiplyArray that takes two inputs:

  1. An array of integers (arr).
  2. A single integer (n).

Your task is to return a new array where each number in the original array is multiplied by n.

If the input array is empty, the function should return an empty array.


Examples:

multiplyArray([1, 2, 3], 2); 
// Output: [2, 4, 6]

multiplyArray([], 5);
// Output: []

multiplyArray([0, 10, -10], 0);
// Output: [0, 0, 0]

Notes:

  • The input array can contain positive numbers, negative numbers, or zero.
function multiplyArray(arr, n) {
  return arr.map((num) => num * n);
}