While loop validation
I came across a question that asks to find the GCD (Greatest Common Divisor) of two numbers. I wrote a code for it and it worked fine but I also checked the solution to look more efficient ways to write it. The solution looks like this: function gcd_two_numbers(x, y) { if ((typeof x !== 'number') || (typeof y !== 'number')) return false; x = Math.abs(x); y = Math.abs(y); while(y) { var t = y; y = x % y; x = t; } return x; } console.log(gcd_two_numbers(12, 13)); // outputs 1 I don't understand understand most part of this code but what's most confusing is the while loop. I know it executes it's code when the condition passed to it is true. But in this case the condition passed to it is y (i.e while(y)), where y = 13. How does this validates? And also y doesn't increments or decrements inside the loop so I don't understand how the while loop iterates. Secondly I don't how they could find the gcd with the code in the loop.