can someone help me understand this code.
Thanks for any help ! Your code so far
function smallestCommons(arr) {
arr.sort((a, b) => b - a);
var newArr = [];
for (let i = arr[0]; i >= arr[1]; i--) {
newArr.push(i);
}
var quot = 0;
var loop = 1;
var n;
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;
}
console.log(smallestCommons([1,7]));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36.
//this creates a function taking the array of numbers [1,7] which are logged bellow
function smallestCommons(arr) {
//this sort the numbers of 1,7 from low to high if it's letters it will go alphabetically
arr.sort
//this compares a and b
//the => means even or more
//so if a and b are equal or more then b-a
((a, b) => b - a);
//a new array is declared it's empty now
var newArr = [];
//for loop the 1 starts at the 0 since 0indexing of the array so if u have a array from 1 through 7 it starts at 1
for (let i = arr[0];
//same happens here but now i is compared to the 2nd number of the array
i >= arr[1];
// i -1
i--) {
//this pushes the new array over i adding i each time
newArr.push(i);
}
var quot = 0; //quote---> 0
var loop = 1;//loop--> 1
var n; //n is a variable now
do { //ok idk what do does??
quot = newArr[0] * loop * newArr[1]; //0 value is now set to the start of the new array x the loop x the 2nd value of new array
for (n = 2; n < newArr.length; n++) {
//condition with for loop
Big picture, the code is checking multiples of the product of smallest number provided (newArr[0]) and the smallest number plus one (newArr[1] = newArr[0] +1) until one of these multiples is divisible by all of the values in the range. do is another way to write a loop.