Scrittura di Algoritmi Intermedio - Minimo Comune Multiplo

Tell us what’s happening:

I wrote an algorithm that works perfectly and passes all the tests on my editor but in the editor of the site does not pass any tests.
I would like feedback from some administrators as I suppose it is a problem of the site and not of my code.

Your code so far

function smallestCommons(arr) {

  // gestione eccezione 0
  if(arr[0] === 0 || arr[1] === 0){
    return 0;
  }

  // creo una lista che comprende l' intervallo dei due indici dell'arr
  lista = [];
  if(arr[0] < arr[1]){
    for(let i = arr[0]; i <= arr[1]; i++){
      lista.push(i);
    }
  }else{
    for(let i = arr[1]; i <= arr[0]; i++){
      lista.push(i);
    }
  }

  // incremementa finquando non trova un numero divisibile per tutti i numeri dell'intervallo
  let multiplo = lista[lista.length - 1];
  console.log(multiplo);
  
  while(true){  
    let divisibile = true;
      for(let i = 0; i < lista.length; i++){
        if(multiplo % lista[i] != 0){
          divisibile = false;
          break;
        }
      }
      if(divisibile){
        break;
      }else{
        multiplo++;
      }
  }

  return multiplo;
}

smallestCommons([1,5]);

Your browser information:

Lo user agent è: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36

Challenge Information:

Scrittura di Algoritmi Intermedio - Minimo Comune Multiplo

This is a problem with you not declarng this variable. The freeCodeCamp editor requires you to properly declare all variables with var, let, or const.


Also, only increasing by 1 here might be slow enough to trigger infinite loop protection in some cases.

1 Like