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

Avoid The One-off Problem, Infinite Loops, Statefulness and Hidden intent.
1.07k stars 49 forks source link

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

Closed KypMon closed 4 years ago

KypMon commented 4 years ago

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.

stevemao commented 4 years ago

@KypMon Thanks a lot!

KypMon commented 4 years ago

@KypMon Thanks a lot!

You are welcome. My pleasure 😆