Is my code good?

Tell us what’s happening:
Hi. My code runs perfect and it works. So this is not a topic asking for help rather to ask if this implementation by me is considered as a good solution? Do i have good progress here or i am programming with not that efficient way?
Your code so far


function smallestCommons(arr) {
arr.sort((a,b)=>a-b);

let smallestCommon=1;
let foundIt = true;
let counter = 0;
while(foundIt){
  for(let i=arr[0];i<=arr[1];i++){
    if(smallestCommon%i==0){
      counter++;
    }
  }
  
  if(counter==(arr[1]-arr[0])+1){
    foundIt=false;
    console.log(smallestCommon);
  }else{
    smallestCommon++;
    counter=0;
  }
}


return smallestCommon;
}


smallestCommons([1,3]);
smallestCommons([1, 13]);
  **Your browser information:**

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

Challenge: Smallest Common Multiple

Link to the challenge:

function smallestCommons(arr) {
  //sort takes time so you do not want use it unless you are actually
  //sorting, but in this case you only have two values so you should
  //just compare them directly as I do
  let i = arr[0] > arr[1] ? arr[0] : arr[1];
  let j = arr[0] < arr[1] ? arr[0] : arr[1];

  //The samllest common can only be as small as our biggest number
  //in range so you that is what you should assume rather than 1;
  let n = i;

  //This is a place holder for i
  let prev = i;

  //with good logic you will evntually gt out of a while loop just 
  //set on true, although I am not vouching for the practice 

  //You really want to avoid a nested loop if possible 
  while (true) {
    //if n does not go into i than increment n, an reset i, and 
    //try again
    if (n % i) {
      i = prev; 
      n++;
    }

    //if n does go into i then decrement i to check othr values in
    //range
    if (n % i === 0) i--;
        
    //if i has not been reset at this point then I know I have
    //found my smallest common multiple      
    if (i < j) break; 
  }
  return n;
}


smallestCommons([2, 10]);

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.