1

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

1

5 Answers 5

5

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

Sign up to request clarification or add additional context in comments.

1 Comment

If this function is not working for you, just change the length t / f to t. I am not sure what this does, but it works for me.
1

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))

Comments

1

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))

Comments

0

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

Comments

0

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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.