Abundant, deficient and perfect number classifications

Tell us what’s happening:

I didn’t know where exactly to post this so I decided on the Support section since I believe this to be a problem with the website.

Using the code I present here I was able to get the correct answer even though this website tells me it doesn’t work for the input num = 20000. I’ve copied the code onto this github page real quick to show that it does indeed give you the correct answer (cornielleandres.github.io/test.html).

I thought it was a problem with overflow but using //noprotect doesn’t seem to work either. I still can’t pass all the tests even though the answer is right.

Your code so far


function getDPA (num) {
  var numArray = [];
  var divArray = [];
  var answerArray = [0,0,0];
  
  for (var x = 1; x <= num; x++) {
    numArray.push(x);
  }
  
  for (var y = 0; y < numArray.length; y++) {
    divArray = [0];
    for (var z = 0; z < numArray[y]; z++) {
      if (numArray[y] % z === 0) {
        divArray.push(z);
      }
    }
    function getSum(total, num) {
      return total + num;
    }
    if (numArray[y] > divArray.reduce(getSum)) {
      answerArray[0] += 1;
    } else if (numArray[y] === divArray.reduce(getSum)) {
      answerArray[1] += 1;
    } else {
      answerArray[2] += 1;
    }
  }
  return answerArray;
}

Your browser information:

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 10575.58.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/coding-interview-prep/rosetta-code/abundant-deficient-and-perfect-number-classifications

Yes, your code works for me in codepen but not on FCC.

In my experience that usually means that your code is taking too long. In codepen, it took 2.5 seconds, which is an eternity. You’re probably meant to find a more efficient algorithm.

Ah, I see. I will try to make it more efficient then. Thanks.