Below is a function that returns simple factors of a given number in JavaScript. I did not write this function, but studied it to expand my knowledge in programming.
My questions are about the while loop, which is inside the next if statement.
if(num % x){
x = 3;
while((num % x) && ((x = x+2) < root));
}
Questions
- What is the purpose of the while loop if there is no code after it?
- What happens when the while loop evaluates to true?
- What happens when the while loop evaluates to false?
Here is the whole function.
function getPrimeFactors(num){
num = Math.floor(num);
var root = 0;
var factors = [];
var doLoop = 1 < num;
var x = 0;
while(doLoop){
root = Math.sqrt(num);
x = 2;
if(num % x){
x = 3;
while((num % x) && ((x = x+2) < root));
}
if(x > root){
x = num;
}else{
x = x;
}
factors.push(x);
doLoop = (x != num);
num = num/x;
}
return factors;
}
Thanks for the help!!!
source
share