you-dont-need/You-Dont-Need-Loops

Got issue running the code, due to wrong position of parameters

KypMon opened this issue · 2 comments

Hi folks,

Thanks for the great works You-dont-need series.
When I try to running the code in the md file, I actually got an issue, for example, like the code sum:

const reduce = function(iterable, reduceFn, accumulator){
  for (let i of iterable){
    accumulator = reduceFn(accumulator, i)
  }
  return accumulator
}

const sum = xs => 
  reduce((acc, x) => x + acc, 0, xs)

sum([1,2,3])  

it throws an error:

TypeError: iterable is not iterable

I found that the xs and anonymous function (acc, x) => x + acc are in the wrong position, to fix it, like below:

const reduce = function(iterable, reduceFn, accumulator){
  for (let i of iterable){
    accumulator = reduceFn(accumulator, i)
  }
  return accumulator
}

const sum = xs => 
  reduce(xs, (acc, x) => x + acc, 0);  // fix here!

sum([1,2,3]) // works!!

Hope it helps.
Thanks.

@KypMon Thanks a lot!

@KypMon Thanks a lot!

You are welcome. My pleasure 😆