Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 −
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 ]
Advertisements