lundi 28 octobre 2019

Great common divisor with loops

I think my logic is wrong but I can't understand where I am making a mistake. I am trying to find the great common divisor, and code will determine what to do if a is bigger than b or b is bigger than a.

I tried many loops, if, while but in the end I deleted all to clear my sight. I made flowchart however it is not coming along with my code.

var a = 64;
var b = 12;
var newA;

while(a > b && newA != 0){

    newA = a - b;

    if(newA === 0){

        outputObj.innerHTML = outputObj.innerHTML + "GCD is " + b;
    }
}



    while(a < b && newA != 0){

        newA = b - a;
    }

    if(newA === 0){

        outputObj.innerHTML = outputObj.innerHTML + "GCD is " + a;

    }
  }

If a is 64 and b is 12 the GCD is 4, if a is 35 and b is 42 the GCD is 7

Aucun commentaire:

Enregistrer un commentaire