Constructing an array of first n multiples of an input number in JavaScript


Problem

We are required to write a JavaScript function that takes in two numbers, let say m and n.

Our function should construct and return an array of first n natural multiples of m.

Example

Following is the code −

 Live Demo

const m = 6;
const n = 14;
const firstNMultiple = (m = 1, n = 1) => {
   const res = [];
   for(let i = 1; i <= n; i++){
      const multiple = m * i;
      res.push(multiple);
   };
   return res;
};
console.log(firstNMultiple(m, n));

Output

[ 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72, 78, 84 ]

Updated on: 20-Apr-2021

302 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements