javascripthigher-order-functionsone-liner

Exponential growth one-liner function fails on one test but not others?


Can anyone debug this for me? I've been staring at it and testing things for hours but can't seem to get it working. I'm starting to wonder whether the coding challenge web app is wrong and I'm right!

The task is this:

Find the number of generations it would take for a population p0 to surpass a max population p given that the population increases by percent% plus aug every generation

My one-liner is as follows:

nbYear = (p0, percent, aug, p) => {
  return Array(999).fill().filter((_,i)=>Array(i+1).fill().map((_,j)=>j==0?p0:0).reduce((y,_)=>Math.round(y*percent/100+y+aug))<=p).length;
}

The code I've written passes on 100 tests but fails on one in particular (they don't disclose the input paramters on which it failed). All it says is that the function gave the output: 51 when it should have been 50


Solution

  • I've concluded that the one test for which it failed must have been for a value over 999.