Smallest Common Multiple(i cant pass)

Smallest Common Multiple(i cant pass)
0

#1

Tell us what’s happening:

i just cant pass this test

Your code so far


function smallestCommons(arr) {
  // return arr;
  let found = false;
  let multiple = 0;
  let integer = Math.max(...arr);
  while(!found){
    let divisible = true;
    for(let i = Math.min(...arr); i <= Math.max(...arr); i++){
      // console.log(`${integer} % ${i} = ${integer % i}`)
      if(integer % i != 0) divisible = false;
    }
    if(divisible === true) {
      found = true;
      multiple = integer;
    };
    integer += Math.max(...arr);
  }
  return multiple;
  console.log(multiple)
}


smallestCommons([23,18]);

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/smallest-common-multiple


#2

Unfortunately, your current algorithm is just not efficient enough to pass that last test. The test suite has a feature which protects against potential infinite loops. When calls against your solution take too long, this feature gets triggered even if you do not have an infinite loop. Once this feature is triggered, your script just stop executing.

See if you can rewrite your solution with a more efficient algorithm.


#3

ok thanks i will try…


#4

ok i get it now

function smallestCommons(arr) {
const min = Math.min(…arr);
const max = Math.max(…arr);
const numSet = [];
//set of numbers
for(let i = min; i <= max; i++){
numSet.push(i)
}
//get multiple of two number
function getMultiple(a, b){
let suggestion = a;
let c = suggestion % b;
while (c != 0){
suggestion += a;
c = suggestion % b;
}
return suggestion;
}
const multiple = numSet.reduce((multiple, num) => {
return getMultiple(multiple, num)
}, max)

return multiple;
}

smallestCommons([1,5]);