Was it helpful?

Question

Constructing multiples array - 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

This will produce the following output on console −

[ 4, 8, 12, 16, 20, 24 ]
raja
Published on 01-Oct-2020 14:03:36
Advertisements
Was it helpful?
Not affiliated with Tutorialspoint
scroll top