下面是一个函数,它返回JavaScript中给定数字的主要因素。 我没有写这个函数,但一直在研究它来扩展我的编程知识。if语句中的while循环的功能是什么? JavaScript
我的问题是关于在以下if语句中的while循环。
if(num % x){
x = 3;
while((num % x) && ((x = x+2) < root));
}
问题
- 什么是while循环的目的,如果有后无码?
- 当while循环评估为真时发生了什么?
- while循环评估为false时发生了什么?
这是整个功能。
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;
}
感谢您的帮助!
它改变'x'的值。 [While](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while)循环将继续循环,直到其条件评估为false。 – Teemu