how to find hcf of 2 numbers in javascript 1

how to find hcf of 2 numbers in javascript

var a = userInput[0].split(" ").map(Number)
while(a[0] != a[1])
{
    if(a[0] > a[1]) { 
        a[0] = a[0] - a[1];
    }
    else {
        a[1] =a[1] - a[0]; 
    }
}
console.log(a[0])

Here is what the above code is Doing:
1. It takes the user input and splits it into an array of two numbers.
2. It then uses a while loop to keep subtracting the smaller number from the larger number until the two numbers are equal.
3. It then prints the number that is left.

Similar Posts