What is the functionality of the while loop inside the if statement? JavaScript
Solution 1
It is really doing something like this:
if(num % x){
x = 3;
while(num % x){
x = x + 2;
if(x < root)
continue;
else
break;
}
}
Except, two is added to x
right in the conditional, so there is no need for a body. The loop will execute until x < root
or num % x
fails to be true. The body just doesn't have any instructions in it.
Its very similar to what happens when you execute a for loop
for(int i=0; i < n; i++)
;
See there are no instructions in the for-loop body, but the loop will still add one to i until i >= n
.
Solution 2
Note the x = x+2
in the while statement. It adds 2 to the value of x
repeatedly until the while clause evaluates true.
So a while loop without a body blocks execution until it's clause becomes true. This is only a good idea if you you are mutating a variable in the clause as part of the condition. Otherwise you may end up in an infinite loop.
Solution 3
This code says, in effect, that if x
is not evenly divisible by 3
, then add 2
to x
and continue if the resulting value is less than root
. The loop terminates as soon as one of these conditions is no longer true, but, meanwhile, x
has been updated along the way.
Note that x = x + 2
evaluates to the assignment's left operand.
Solution 4
There is actually one thing happening in the loop:
V
while((num % x) && ((x = x+2) < root));
x = x + 2
is an assignment, you can see it is not a ==
or ===
operator but a =
.
(x = x + 2) < root
means increment x
by 2
, and then compare it to root
.
Using an assignment in the condition part of a while
or if
statement is generally not recommended, because it makes the code less readable.
Solution 5
The while
statement there does change the value of the x
variable. It could be rewritten as something like the following:
if (num % x) {
x = 3;
if (num % x){
do {
x = x + 2;
} while (num % x && x < root);
}
}
JaredRogers
Updated on June 07, 2022Comments
-
JaredRogers almost 2 years
Below is a function that returns the prime factors of a given number in JavaScript. I did not write the function but have been studying it to extend my programming knowledge.
My questions are about the while loop that is inside the following if statement.
if(num % x){ x = 3; while((num % x) && ((x = x+2) < root)); }
Questions
- What is the purpose of a while loop if there is no code after it?
- What is happening when the while loop evaluates true?
- What is happening when the while loop evaluates false?
Here is the function in it's entirety.
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!!!
-
kapa about 10 yearsWell, actually not.
(num % x)
will use the currentx
, and(x = x+2) < root)
will use the incrementedx
. -
Robert Westerlund about 10 yearsStill not entirely correct. The
x
variable in the original code is updated, even ifx + 2 < root
while this example does not changex
in that case. -
Joel about 10 yearsMy apologies for not spending tons of time guaranteeing the correct output, I was more concerned with explaining the concept to the OP.
-
Robert Westerlund about 10 yearsIn the original code, 2 is added to
x
even ifx + 2
is less thanroot
, since that addition is performed before comparing it withroot
. -
Robert Westerlund about 10 yearsThat is a good approach. We only want to help make your explanation more accurate.
-
Teemu about 10 yearsActually when
x
reaches or exceedsroot
, or untillnum % x === 0
. -
kapa about 10 yearsI would be happy to improve my answer if the downvoter expressed his reasons.
-
JaredRogers about 10 yearsNo, it wasn't a beginners book. I have been doing Project Euler problems and when I get stuck I search for pieces of functionality that I wasn't able to do on my own. Then I tear them apart and evaluate them to expand my knowledge on the subject.
-
JaredRogers about 10 yearsThe purposed solution does not work with all numbers. Specifically when you input 144. Your example outputs [2, 2, 2, 2, 9] and it should output [2, 2, 2, 2, 3, 3].
-
JaredRogers about 10 yearsI totally missed the = (assignment) vs == (comparison). I was thrown because I have never seen a loop without a body. Thank you this solution works properly and is easier to visualize.
-
Joel about 10 years@JaredRogers quite honestly, I am fine with that. There is no need to rewrite the code if it is functional. I was just trying to help you understand what is happening. Notice I prefaced the code with something like this.
-
Joel about 10 years@JaredRogers My latest edit should reflect an accurate expansion of the code.
-
JaredRogers about 10 years@Joel Thanks for updating your response to accurately represent the same functionality. I get what you were saying about getting the gist of it but I think the details are important. If an example doesn't accurately represent something it can cause more misunderstanding. Again thank you for your time and the explanation.
-
Wayne about 10 yearsPoorly phrased. I'll re-word.