Hello all. I’m currently working on the smallest common multiple challenge, but need some help explaining the code that is on the wiki. I understand how the code works I just don’t understand why it’s there. The code on the wiki is
function smallestCommons(arr) {
// Sort array from greater to lowest
// This line of code was from Adam Doyle (http://github.com/Adoyle2014)
arr.sort(function(a, b) {
return b - a;
});
// Create new array and add all values from greater to smaller from the
// original array.
var newArr = [];
for (var i = arr[0]; i >= arr[1]; i--) {
newArr.push(i);
}
// Variables needed declared outside the loops.
var quot = 0;
var loop = 1;
var n;
// Run code while n is not the same as the array length.
do {
quot = newArr[0] * loop * newArr[1];
for (n = 2; n < newArr.length; n++) {
if (quot % newArr[n] !== 0) {
break;
}
}
loop++;
} while (n !== newArr.length);
return quot;
}
// test here
smallestCommons([1,5]);
I understand the reasoning for everthing in the code until the author introduces the do
. If someone has some time, I would really apprecieate a detailed explanation for the construction of this loop, so as the challenges get harder, loops of this caliber become easier to recognize and form. Best.