# Edit in JSFiddle

```// Find the GCD of these four integers...
// Replace with as many as you like...
var ofTheseIntegers = [30, 100, 25, 75];

// Invoke the function...
var firstPart = 'The Greatest Common Divisor<br />of these four integers:<br />' + ofTheseIntegers[0] + ', ' + ofTheseIntegers[1] + ', ';
var secondPart = ofTheseIntegers[2] + ' and ' + ofTheseIntegers[3] + ' is <span style="color:red"><big>' + getTheGCD(ofTheseIntegers) + '</big></span>';
document.getElementById("demonstration").innerHTML = firstPart + secondPart;

var terms = [];

function getTheGCD(terms) {

var q; // for debugging
var i; // for incrementing conditional 'for' loops
var v; // for incrementing conditional 'for' loops

// Quantity of integers whose GCD is to be sought...
var count = terms.length;

// Last position of an integer within 'terms'...
var last = count - 1;

// Sort the contents of 'terms'...
terms.sort(function(a, b) {
return a - b;
});

// This has to be at least one greater than the largest integer in the array: 'terms'.
// Otherwise, the sort function will push all the numbers to the top of the 'terms' array
// and begin to cut them out a little at a time!
var numeric_padding = terms[last] + 1;

// Permanently save the quantity of integers...
var save_count = count;

// Temporarily save the quantity of integers...
var kount = count;

// Establish a second array, 'remains', to swap with the contents of 'terms' and
// continue to swap them back and forth to each other throughout the 'while' loop below...
var remains = [];
remains[0] = 0;

// Perform our first "shift to the right" of the contents of 'terms'...
for (i = 0; i < (count - 1); i++) {
remains[i + 1] = terms[i];
}

// Uncomment the following line for debugging...
//for (q = 0; q < 4; q++)

// ...and comment out the next line for debugging...
while (kount > 1) {
// Transfer 'count' to 'kount' since neither variable will retain
// their values throughout the iterations of the 'while' loop above...
count = kount;

// Take the modulo remainder of 'terms' and store it in 'remains'...
for (i = 0; i < save_count; i++) {
if (i === 0) {
remains[i] = terms[i];
} else {
remains[i] = terms[i] % remains[i];
}
}

//	if (q == 3) { break; } // for debugging

// Fill in empty slots, within 'terms', using padded values greater than
// the largest integer to help with properly sorting 'terms'...
for (v = 0; v < save_count; v++) {
}

// Reset 'kount' to zero...
kount = 0;

// Transfer 'remains' to 'terms' and 'kount' the number of non-zero 'terms'...
for (i = 0; i < count; i++) {
if (remains[i] !== 0) {
terms[i] = remains[i];
kount++;
}
}

if (kount > 1) {
// Sort the contents of 'terms'...
terms.sort(function(a, b) {
return a - b;
});
remains[0] = 0;

// Transfer 'terms' to 'remains' by shifting, or offsetting, this transfer to the right of the first array position...
for (v = 1; v < save_count; v++) {
remains[v] = terms[v - 1];
}
}

//		if (q == 5) { break; } // for debugging
//		document . write ('*' + kount + '*'); // for debugging
}
}

```<div align="center">