 # Smallest Common Multiple - help neede!

Hi
I’m trying to use the array.forEach() function in order to test whether all the numbers in the array are divisible by multiples of the largest number in the array.

Eg. arr = [4, 3, 2]
Check if multiples of 4 (4, 8, 12) are divisible by 3 and 2.

My code so far is this. I don’t know what to put in the while() condition though. Any ideas?

``````function smallestCommons(arr) {
//convert arguments into array
arr = Array.prototype.slice.call(arguments);
arr = arr;

//sort the array from highest to lowest number
function compareNumbers(a, b){
return b-a;
}
arr.sort(compareNumbers);
console.log(arr); //this is the lowest number in the original array

//fill the array with numbers between the lowest and highest numbers. first check how many numbers there will be in the whole array.
var inArr = arr-arr+1;
console.log("number of elements in new array: " + inArr);//this is the number of elements in the array (original elements + new elements)

var x;
for (x = 1; x < inArr; x++){
arr[x] = arr[x-1] - 1;
}
console.log("new array: " + arr); //the new array containing original elements + new elements

var mult = 1;
var common = 0;
function isDivisible(el){
common % el === 0;
}
do{
common = arr*mult;
if (arr.every(isDivisible)){
return common;
}
mult++;
} while(???);
``````

What condition in the do…while loop do you think makes sense for when it should keep looping? Once you figure that how, then trying coding that condition.

Initially I had an empty array, let`s call it commonArr. when the variable common is returned, i will push the value of common into that empty array. thus, while (commonArr.length === 0). The code was like this, but the result i got was “undefined”.

var mult = 1;
var common = 0;
var commonArr = [];
function isDivisible(el){
common % el === 0;
}

do{
console.log(commonArr);
common = arr*mult;
if (arr.every(isDivisible)){
commonArr.push(common);
return common;
}
mult++;
} while(commonArr.length === 0);

Your function isDivisible needs to return a true or false value.

awww snap!!! Thank you so much, it works now! I was able to take your solution with the correction I mentioned to you and remove a couple of unneeded variables, plus I simplified the while condition.

``````function smallestCommons(arr) {
arr.sort((a,b) => b-a);
for (var numbers = [], num = arr; num >= arr; num--)
numbers.push(num)
var mult = 1;
do{
var common = numbers*mult;
if (numbers.every(number => common % number === 0))
return common;
mult++;
} while(true);
}
``````

I will study your solution. I struggle with understanding arrow functions. Any tip on how to easily interpret it?

Here is my same solution written without using arrow functions. Now you can compare the two.

``````function smallestCommons(arr) {
arr.sort(function(a,b) { return b-a });
for (var numbers = [], num = arr; num >= arr; num--)
numbers.push(num)
var mult = 1;
do{
var common = numbers*mult;
if (numbers.every(function(number) { return common % number === 0 }))
return common;
mult++;
} while(true);
}
``````

One more thing to ponder is how you could have rewritten your while loop and made it a for loop:

``````  for (var mult = 1; ;mult++) { // notice there is no for loop condition (blank between the two semi-colons)
var common = numbers*mult;
if (numbers.every(function(number) { return common % number === 0 }))
return common;
}
``````

To help clarify while doing the comparison:

-WWC

Very nice and concise. Didn’t realize you could declare a variable (var = numbers []) inside the for (…).

Thank you! I’ve read this, but it still doesn’t come to me naturally. It`s a matter of forcing myself to use it perhaps…

so if there’s no condition, would there be a risk that the loop will run infinitely?

I just realized you didn’t have to convert the arguments into an array. I think I got confused with another challenge that asked us to do this… how do you know when you have to do that?

I don’t understand what while(true) means here. If you wouldnt mind elaborating, that would be great.Thanks again!

It is just the do… while loop condition. As long as the condition evaluates to true, the loop keeps going. By putting the value true as the condition, I guarantee it will keep looping until the return statement executes.

1 Like

In this challenge arr was already an array. In another challenge where you have a function which can received an unknown number of arguments, you might want to convert those arguments into an array, so you could use `Array.prototype.slice.call(arguments)` to do that.

1 Like
``````
function smallestCommons(arr) {

var nums = [];
var total=1;

//Sort
if(arr>arr){
arr.sort(function(a,b){
return b-a;
});
}

//Array of numbers included
for(i=arr;i>=arr;i--){
nums.push(i);
}

//For every element of array
for(var z=nums.length-1;z>=0;z--){
//set total as the greatest number
if(z===(nums.length-1)){
total*=nums[z];
}
//if the upcoming number cant divide total then multiply
if(total%nums[z]!==0){
total*=nums[z];
}
}