Alfrex92
Alfrex92

Reputation: 6768

Return Multiples of a Number within a range with Javascript

I would like to create a function that returns an array of multiples of a number within an array.

The first argument is a number and the second is the max number of the range.

For example:

getMultiples(5,24)

Output: [5,10,15,20]

*If the range is less than the first argument it should return an empty array.

getMultiples(5,0)

Output: []

Any idea how can I achieve this? Help

Upvotes: 1

Views: 1401

Answers (5)

Ben Aston
Ben Aston

Reputation: 55729

The following code creates a generator function for the series up to limit; this is then spread across an array:

function* multiples(n, lim) {        
    if (lim < n) return []
    let i = 1, r = 0
    while ((r = n * i++) < lim) yield r
}

const getMultiples = (n, lim) => [...multiples(n, lim)]    

console.log(getMultiples(5, 24))
console.log(getMultiples(5, 0))

Alternative: create an array of the correct length, then use the index argument of map to populate the array:

const getMultiples = (n, lim) =>        
    lim < n
        ? []
        : [...Array(~~(lim/n))].map((_,i) => ++i*n)

console.log(getMultiples(5, 24))
console.log(getMultiples(5, 0))

Using a for-loop:

const getMultiples = (n, lim) => {
    const arr = [], l = ~~(lim/n)
    for(let x = 1; x <= l; x++) 
        arr.push(x*n)
    return arr    
}

console.log(getMultiples(5, 24))
console.log(getMultiples(5, 0))

Using a for-of loop:

const getMultiples = (n, lim) => {
    const arr = Array(~~(lim/n))
    for(let x of arr.keys()) 
        arr[x] = (x+1)*n
    return arr    
}

console.log(getMultiples(5, 24))
console.log(getMultiples(5, 0))

Upvotes: 1

kind user
kind user

Reputation: 41893

One of possible solutions:

const getMultiples = (f, t) => 
   [...(Array(Math.floor(t / f)))]
   .map((_, i) => f * (i + 1));

console.log(getMultiples(5, 24));
console.log(getMultiples(3, 11));
console.log(getMultiples(7, 1));

Or extra one-liner:

const getMultiples = (f, t) => 
   Array.from({ length: t / f }, (_, i) =>  f * (i + 1));

console.log(getMultiples(5, 24));

Thanks @Rajesh

Upvotes: 5

Gautam
Gautam

Reputation: 81

This should work:

getMultiples = (num, range) => {
  
  if(range < num) {
    return [];
  }
    
 const resultArr = [];
  let result = 0;
  let itr = 1;
 
  while(result < range) {
    result = num*itr;
    if(result < range) {
       resultArr.push(result);
    }
    itr++;
  }
 
  return resultArr;
}

console.log(getMultiples(5,24));

Upvotes: 0

Ilijanovic
Ilijanovic

Reputation: 14904

Here an solution with while

function getMultiples(numb, sum) {
   let count = 1;
   let result = [];
   while(numb * count <= sum) count = result.push(numb * count)
   return result;
}

console.log(getMultiples(5, 23))

Upvotes: 1

Alpha Wolf Gamer
Alpha Wolf Gamer

Reputation: 327

function getMultiples(num1, num2) {
  var check = true;
  var number = 0;
  var arr = []
  while (check == true) {
    if (number <= num2) {
      arr.push(number)
      number += num1;
    } else {
      check = false
    }
  }
  console.log(arr)
}

getMultiples(5, 24);

I cant figure out on how to remove the 0 but I am sure you can figure that out

Upvotes: 0

Related Questions