What is the functionality of the while loop inside the if statement? JavaScript

14,129

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);
    }
}
Share:
14,129
JaredRogers
Author by

JaredRogers

Updated on June 07, 2022

Comments

  • JaredRogers
    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

    1. What is the purpose of a while loop if there is no code after it?
    2. What is happening when the while loop evaluates true?
    3. 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
    kapa about 10 years
    Well, actually not. (num % x) will use the current x, and (x = x+2) < root) will use the incremented x.
  • Robert Westerlund
    Robert Westerlund about 10 years
    Still not entirely correct. The x variable in the original code is updated, even if x + 2 < root while this example does not change x in that case.
  • Joel
    Joel about 10 years
    My apologies for not spending tons of time guaranteeing the correct output, I was more concerned with explaining the concept to the OP.
  • Robert Westerlund
    Robert Westerlund about 10 years
    In the original code, 2 is added to x even if x + 2 is less than root, since that addition is performed before comparing it with root.
  • Robert Westerlund
    Robert Westerlund about 10 years
    That is a good approach. We only want to help make your explanation more accurate.
  • Teemu
    Teemu about 10 years
    Actually when x reaches or exceeds root, or untill num % x === 0.
  • kapa
    kapa about 10 years
    I would be happy to improve my answer if the downvoter expressed his reasons.
  • JaredRogers
    JaredRogers about 10 years
    No, 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
    JaredRogers about 10 years
    The 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
    JaredRogers about 10 years
    I 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
    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
    Joel about 10 years
    @JaredRogers My latest edit should reflect an accurate expansion of the code.
  • JaredRogers
    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
    Wayne about 10 years
    Poorly phrased. I'll re-word.