Problem 21: Amicable numbers: Performance issues

I get the right output in the console but I believe the performance is a bit too slow to pass. What can I do to optimize my solution?

I also was confused about why there would be duplicates in my amicNums. I saw this when I played with the solution in the console and realized this was what was giving me the wrong output originally so I added amicNums.indexOf(i) === -1 in the if-statement to fix this…however, I’m still not sure why there would be any duplicates?

Your code so far

function sumAmicableNum(n) {
  function d(n){
    let output = []
    for (let i = 1; i < n; i++){
        if (n%i===0){
    if (output.length === 0){
      return 0
    }else {
      return output.reduce((a,b)=> a+=b)

  let amicNums = []
  for (let i = 1; i < n; i++){
    let r1 = d(i)
    let r2 = d(r1)
    if (i === r2 && i !== r1 && amicNums.indexOf(i) === -1){
      amicNums.push(i, r1)
  return amicNums.reduce((a,b)=> a+=b)


Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36.

Link to the challenge:

I tested your code but it would only work on the first number which is the 1000 but the rest does not work like for 2000, 5000 and 10000. Did I missed something?

Also, this is my first time to see this problem and I tried the 220 example it worked fine but with 1000 or 2000 for example it does not work on my case. Where did they get the 2898 for 2000 (for example) when the sum for all proper divisors is only 2836 in my case? And those numbers are 1,2,4,5,8,10,16,20,25,40,50,80,100,125,200,250,400,500,1000.