Array of multiples - JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

We are required to write a JavaScript function that takes in two numbers, say m and n, and it returns an array of first n multiples of m.

For example − If the numbers are 4 and 6

Then the output should be −

const output = [4, 8, 12, 16, 20, 24]

Example

Following is the code −

const num1 = 4;
const num2 = 6;
const multiples = (num1, num2) => {
   const res = [];
   for(let i = num1; i <= num1 * num2; i += num1){
      res.push(i);
   };
   return res;
};
console.log(multiples(num1, num2));

Output

Following is the output in the console −

[ 4, 8, 12, 16, 20, 24 ]
raja
Updated on 16-Sep-2020 09:32:46

Advertisements