I am creating a web page that will find the prime factorization of a number. The function I use calls itself from within a loop. After I execute the function within itself, the index of the loop is changed from the index that it reached from the second calling. It should return all prime factors in an array. Here is the code.
function pf(num){
max=Math.floor(Math.sqrt(number));
factors=[];
prime=true;
for(count=2;count<=max;count++){
ratio=num/count;
if(ratio%1==0){
alert(count);//HERE
factors=pf(ratio);
alert(count);//HERE
factors.push(count);
prime=false;
break;
}
}
if(prime){
factors.push(num);
}
return factors;
}
Say this code runs with an input of 20. The first alert will show the number 2, but the next one will show 3. Is there a way to make the current index of the loop not changed by the second calling of the function?