0

I am trying to filter an array using the some function as a callback and have only elements which are multipliers of the other array.

function sumOfNumbersDivisibleByAnyFunctional(upTo) {
  var numbers = [];
  for(var i = 0;i<=upTo; i +=1){
    numbers.push(i);
  }
  function filterByArr(arr){
    return function (el){
      return arr.some(multiplier => el % multiplier === 0);
    }
  }
return numbers.filter(filterByArr(arguments));
}

But no matter how I tried I am getting an error saying the arr.some is not a function. Any idea what I am missing?

I also tried:

return numbers.filter(noToTest => arguments.some(multiplier => noToTest % multiplier === 0));

If this is called:

sumOfNumbersDivisibleByAnyFunctional(10,3,5) 

it will create an array of numbers between 0-10 and return only ones that are multipliers of 3 or 5.

I don't this this should be closed as duplicate of How can I convert the “arguments” object to an array in JavaScript?. As mentioned this is the issue with the code above but does not answer the original question. for reference I would leave the question open and post n answer explaining the correct solution.

Eran Witkon
  • 4,042
  • 4
  • 19
  • 20
  • 1
    Can you share some sample inputs and outputs? – gurvinder372 Jan 03 '18 at 12:02
  • "error saying the arr.some is not a function. Any idea what I am missing?" — You're missing an array. `arr` isn't one. – Quentin Jan 03 '18 at 12:08
  • `arguments` is not an array, it does't have `some` method – Tomasz Bubała Jan 03 '18 at 12:08
  • Just change your `filterByArr()` method to `function filterByArr(arr){ arr = Array.from(arr); arr.shift(); return function (el){ return Array.from(arr).some(multiplier => el % multiplier === 0); } }` – gurvinder372 Jan 03 '18 at 12:08
  • And don't declare function statements inside other functions. Use function expressions `var fn = function() { blah, blah }`. – Jared Smith Jan 03 '18 at 12:08
  • Please, can you provide an example of input data and desired result (filtered data)? – Alexander Elgin Jan 03 '18 at 12:12
  • issue was solved by @Quentin, the issue is not with the filter or some function but with the way to work with `arguments`. when the last line is changed to `numbers.filter(filterByArr([].slice.call(arguments)));` it works as expected. – Eran Witkon Jan 03 '18 at 12:17
  • 1
    @JaredSmith — There is nothing wrong with using a function declaration inside a function – Quentin Jan 03 '18 at 12:19
  • 1
    @Quentin `filteredByArray` is declared inside `sumOfNumbersDivisibleByAnyFunctional`. It's just hard to tell because of the lack of formatting. As for whether or not that's a good idea, YMMV. – Jared Smith Jan 03 '18 at 12:22

0 Answers0