Smallest Common Multiple - help neede!

Smallest Common Multiple - help neede!
0.0 0

#1

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[0];

  //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[0]-arr[1]+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[0]*mult;
    if (arr.every(isDivisible)){
      return common;
    }
    mult++;
  } while(???);

#2

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.


#3

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[0]*mult;
if (arr.every(isDivisible)){
commonArr.push(common);
return common;
}
mult++;
} while(commonArr.length === 0);


#4

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


#5

awww snap!!! Thank you so much, it works now! :nerd_face:


#6

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[0]; num >= arr[1]; num--)
    numbers.push(num)
  var mult = 1;
  do{
    var common = numbers[0]*mult;
    if (numbers.every(number => common % number === 0))
      return common;
    mult++;
  } while(true);
}

#7

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


#8

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[0]; num >= arr[1]; num--)
    numbers.push(num)
  var mult = 1;
  do{
    var common = numbers[0]*mult;
    if (numbers.every(function(number) { return common % number === 0 }))
      return common;
    mult++;
  } while(true);
}

#9

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[0]*mult;
    if (numbers.every(function(number) { return common % number === 0 }))
      return common;
  }  

#10

To help clarify while doing the comparison:

-WWC


#11

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


#12

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…


#13

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


#14

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?


#15

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


#16

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.


#17

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.


#18

function smallestCommons(arr) {
  
  var nums = [];
  var total=1;

  //Sort 
  if(arr[1]>arr[0]){
    arr.sort(function(a,b){
      return b-a;
    });
  }
  
  //Array of numbers included
  for(i=arr[0];i>=arr[1];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];
    }
  }
  
  
  return total;
}


smallestCommons([1,5]);

the last part of my code doesnt work. can anybody realize the error?