4

Create a function called multiplesOf.
It will accept two arguments, the first will be an array of numbers, the second will be a number.
The function should return a new array that is made up of every number from the argument array that is a multiple of the argument number.
So multiplesOf([5,6,7,8,9,10], 3) will return [6, 9].

function multiplesOf(numbers) {
  var multiples = numbers[0];

  for (var i = 0; i < numbers.length; i++) {
    if (numbers[i] % multiples === 0) {
      multiples = numbers[i];
    }
  }

  return multiples;
}

console.log(multiplesOf([4, 5, 6, 7, 8], 2));

says function is defined in jsbin help please

greybeard
  • 2,249
  • 8
  • 30
  • 66
Dalton
  • 127
  • 1
  • 2
  • 7

10 Answers10

11

You have a few issues with your code. At the moment multiplesOf only accepts 1 argument when it should be two, ie, the numbers array and a single number

Your other issue is that you are not keeping an array of the multiples found, instead, you are setting a variable to the multiples found and is getting overwritten when a new multiple if found (thus leaving you with the very last multiple found in the array). Instead, you want to change your multiples variable to an array. This way you can push every multiple found into this array.

See working example below (read code comments for changes):

function multiplesOf(numbers, number) { // add second argument
  var multiples = []; // change to array (so that we can store multiple numbers - not just one multiple)
  for (var i = 0; i < numbers.length; i++) {
    if (numbers[i] % number === 0) { // divide by the number
      multiples.push(numbers[i]); // add the current multiple found to the multiples array
    }
  }

  return multiples;
}

console.log(multiplesOf([4, 5, 6, 7, 8], 2)); // Output: [4, 6, 8]

Or, if you are happy to use a higher-order function, you can also use .filter() to get your new array. .filter() accepts a function as its first argument which takes an element as its argument. It will keep any of the elements which you return true for in your new array:

const multiplesOf = (numbers, number) => numbers.filter(n => !(n % number));
console.log(multiplesOf([4, 5, 6, 7, 8], 2)); // [4, 6, 8]
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
3

You can use the filter method to achieve this:

const multiplesOf = (numbers, multiple) => {
return numbers.filter(x => x % multiple === 0);
}

console.log(multiplesOf([4, 5, 6, 7, 8], 2));

The filter method will return the array after filtering it with your specified condition which in our case is to check for a multiple.

You were also missing the 2nd argument to the function which is the multiple.

Dilek
  • 83
  • 8
Hussain Ali Akbar
  • 1,585
  • 2
  • 16
  • 28
2

Issue is this: return multiples;

You are returning a single value. A function can return a single value, so you will have to create an array and then return it

function multiplesOf(numList, num) {
  return numList.filter(function(n) { return n % num === 0; })
}

console.log(multiplesOf([4, 5, 6, 7, 8], 2));
Rajesh
  • 24,354
  • 5
  • 48
  • 79
2

You need to pass the second argument to the function. Also,var multiples = numbers[0]; is not necessary. Instead of doing multiples = numbers[i]; you will have to push the right values inside of the array

function multiplesOf(numbers, num) {
  var multiples = []

  for (var i = 0; i < numbers.length; i++) {
    if (numbers[i] % num === 0) {
      multiples.push(numbers[i]);
    }
  }

  return multiples;
}

console.log(multiplesOf([4, 5, 6, 7, 8], 2));

Alternatively, you can also use array.reduce function:

function multiplesOf(arr, num) {

  return arr.reduce((acc, curr) => {
    if (curr % num === 0) {
      acc.push(curr)
    }
    return acc;
  }, [])

}

console.log(multiplesOf([4, 5, 6, 7, 8], 2));
Dilek
  • 83
  • 8
brk
  • 48,835
  • 10
  • 56
  • 78
2

I assume is that what you want

var result = []

function arrMul(arr, value) {
  for (var i = 0; i < arr.length; i++) {
    if (arr[i] % value === 0) {
      result.push(arr[i])
    }

  }
  return result
}
console.log(arrMul([4, 5, 6, 7, 8], 2))
p u
  • 1,395
  • 1
  • 17
  • 30
2

Here is a one liner function that uses reduce. I have also used comma operator to return the result arr.

var multiplesFn = (numberArr, num ) => numberArr.reduce((arr, curVal) => ( curVal%num == 0 && arr.push(curVal), arr),[])

console.log(multiplesFn([2,33,34,22,44,6,8], 2))
Shubham Singh
  • 143
  • 1
  • 1
  • 7
1

You need to add second parameter to the function multiplesOf(numbers, divider). You can simply create an array and push multiples to that array and return the array.

function multiplesOf(numbers, divider) {
  // create an empty array
  var  multiples = []
  for (var i = 0; i < numbers.length; i++) {
    if (numbers[i] % divider === 0) {
     // push multiples to the array
      multiples.push(numbers[i]);
    }
  }
 // return the array
  return multiples;
}

console.log(multiplesOf([4, 5, 6, 7, 8], 2));
Sahil Raj Thapa
  • 2,353
  • 3
  • 12
  • 23
0
console.log(sum(10));
function sum(limit) {
    let sum = 0;

        for (let i = 0; i <= limit; i++)
            if (i % 3 === 0 || i % 5 === 0)
                sum += i;

    return sum;
}

//enjoyit
sdfsdf
  • 1
  • While this code may provide a solution to the question, it's better to add context as to why/how it works. This can help future users learn, and apply that knowledge to their own code. You are also likely to have positive feedback from users in the form of upvotes, when the code is explained. – borchvm Aug 10 '20 at 08:31
0

This code is for people who don't want to add an array. I know the question asked for two arguments, an array and a number for multiples, but without an array goes like this.

function multiples(limit, n) {
    return Array(limit).fill(0).map((v,i)=>{return (i+1)%n===0 ? i+1 : 0}).filter(v=>v!=0)
}

Confusing ES6, right? First, let me explain the map function in code.

Array.prototype.map = (callback) {
    let out = []
    this.forEach((v)=>{
        out.push(callback(v))
    }
    return out
}

Basically, it runs a function on every value in an array, and returns a new array with the results of the function.

Filter is very simple. It passes you the value, and index, and you can run a function on it. If the function returns true, the value is included, but if it returns false, the value is excluded from the new array being made.

sld
  • 93
  • 10
0

There are many method/ways to find. you can use reduce or filter method of array. I think filter method is easiest way to find it.

const multiplesOf = (array, num) =>{
return array.filter(item => item % num === 0)
}
console.log(multiplesOf ([4, 5, 6, 7, 8], 2)) // you will get[4,6,8]