初级
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.执行代码,当n不是和数组长度相等时
do {
quot = newArr[0] * loop * newArr[1];//令quot等于新数组的第一个元素乘以loop变量再乘以新数组的第二个元素
for (n = 2; n < newArr.length; n++) {//执行循环,从n到数组长度
if (quot % newArr[n] !== 0) {//如果数组中有一个元素无法被quot整除的话,就结束循环
break;
}
}
loop++;
} while (n !== newArr.length);
return quot;
}
// test here
smallestCommons([1,5]);