Javascript solution using Math.sqrt and Array.unshift


#1

My solutions follows the video by using the Math.sqrt() function for my iterator, and then I hold two separate arrays for the “first factor” and then its pair. The pairs will be found in decreasing order, so I use Array.unshift instead of Array.push, and just concat these two at the end. Not the most efficient for memory I’m sure, but was an easy way to think about it for me.

let firstFactors = [];
let secondFactors = [];
    for (let i = 1; i <= Math.sqrt(A); i++) {
        if (A % i === 0) {
            firstFactors.push(i);
            if (i !== Math.sqrt(A)) {
                secondFactors.unshift(A/i);
            }
       }
   }
  return firstFactors.concat(secondFactors);

#2

But unshift itself will have complexity as equal to the length of secondFactors array