Lowest Common Multiple

I have managed to get passed most of the intermediate challenges but these last 3 maths-based ones I have struggled.

Is there anything I can research to help me understand what I’m doing without looking directly at the solution. So far I have sorted the array and got the range from greatest to smallest. My next plan was to loop through the sorted array and use a nested loop to do something like if (i % j === 0) if ( j < smallestMultiple) smallestMultiple = j;

Thanks, if i need to go back to earlier lessons and understand more of the basics to getting this I will do so, I felt the previous Primes challenge was a big jump from the challenges before that.

Your code so far


function smallestCommons(arr) {
 arr.sort((a,b) => a -b);
let max = Math.max(...arr);
let min = Math.min(...arr);
let count = min + 1; 
let numbers = arr.sort((a,b) => b-a)

}

console.log(smallestCommons([1, 5]))

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36.

Challenge: Smallest Common Multiple

Link to the challenge:

try googling algorithms to find smallest common multiples, you could find them on wikipedia or on maths focused places, then you just need to convert it to JavaScript - do not invent an already existing wheel :wink:

Thanks, i’ll try my best to research an algorithm then put it into code. I’ll keep this thread open to post my inevitable struggle! :smiley:

So after a good research of algorithms and quite a bit of guidance on Youtube, I have come up with this.

 function smallestCommons(arr) {
  arr.sort((a,b) => a -b);
 let max = Math.max(...arr);
 let min = Math.min(...arr);
 let count = min + 1; 
 let numbers = arr.sort((a,b) => b-a)
 let result = numbers [0];

 for (let i = 1; i < numbers.length; i++) {
    result = findLCM(result, numbers[i]);

 }
 return result;
 
}

function findLCM(x, y) {
  var max; 
  max = (x > y) ? x:y;
 
  while (true) {
if (max % x ===0 && max % y ===0) {
  return max;
  

}
max ++

  }
  


}

console.log(smallestCommons([1, 5]))

I’m still not getting the right answer though, any ideas?

I’ve also just noticed I can delete some variables from this code that I was using for a previous attempt which I have now abandoned.

what are you trying to do here?

use console.log statements to verify that the value of everything is what you expect

I should of deleted the count variable, I dont think i need it anymore. The numbers array was to sort the code from greatest to smallest, Ive only just noticed that mistake that there is a gap between numbers and [0] I was intended to grab the first element of the array.

yes, but that’s not the issue

you need to find the smallest common multiple between which numbers?

function smallestCommons(arr) {
  arr.sort((a,b) => a -b);
 let max = Math.max(...arr);
 let min = Math.min(...arr);
  let numbers = arr.sort((a,b) => b-a)
 let result = numbers[0];

 for (let i = 1; i < numbers.length; i++) {
    result = findLCM(result, numbers[i]);

 }
 return result;
 
}

function findLCM(x, y) {
  var max; 
  max = (x > y) ? x:y;
 
  while (true) {
if (max % x ===0 && max % y ===0) {
  return max;
  

}
max ++

  }
  


}

console.log(smallestCommons([1, 5]))

The lowest common multiples of each number in the array, Is this not a correct approach if i compare two numbers in the array using the findLCM function?

I see what you mean now there is now no range of numbers in the array and only [5,1]

you can also reduce this all to 2-3 lines, and use the loop to go through the range

Thanks @ieahleen, that was tough! now solved.
What would be the best way to refactor the start of the code that you mention above? here is my solution


function smallestCommons(arr) {
  arr.sort((a,b) => a -b);
 let max = Math.max(...arr);
 let min = Math.min(...arr);
 let count = min +1;
 while (count < max) {
   arr.push(count)
   count++
 }
  let numbers = arr.sort((a,b) => b-a)
  console.log(numbers)
 let result = numbers[0];

 for (let i = 1; i < numbers.length; i++) {
    result = findLCM(result, numbers[i]);

 }
 return result;
 
}

Does this get problem-solving get easier? the code makes complete sense to me and I understand every line. But i am really struggling with coming up with the logic myself to create a working solution and i was not making any progress trying to do it completely by myself.

all of this… could you keep only min and max and adapt the loop to still work? that would make this so much more tidy…

Does this seem more readable?

 for (let i = Math.min(...arr) +1; i < Math.max(...arr); i++) {
     arr.push(i)
    arr.sort((a,b) => b -a)
    
    console.log(arr)

 }
 

Tried to combine the sort and push together, but says not a function. Also deleted the numbers array as it was not in the correct scope after refactoring.

arr.sort(...)

for (let i = arr[0]; i <= arr[i]...