Error on "Smallest Common Multiple" e

Error on "Smallest Common Multiple" e
0

#1

My solution to this problem is this:


function smallestCommons(arr) {
  let start = Math.min(...arr);
  let end = Math.max(...arr);
  let number = end;
  let stop = false;
  console.log("start", start, "end", end, "number", number);

  while(!stop){
    let valid = true;
    for(let i=start; valid && (i <= end); i++){
      valid = (number % i === 0);
    }
    if(valid)
      stop = true;
    else
        number++;
  }
  console.log("Arr", arr, "Number:", number);
  return number;
}

I tried out directly in the browser and it is working correctly but when I run on FCC it fails for [1, 13] and [23,18].


#2

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.


#3

It appears your solution is not efficient enough, which means it is taking longer than FCC expects. When this happens, a built-in infinite loop protection features stops your code from continuing. You do not have an infinite loop, but the test thinks you do. To pass the challenge, you will need to think about how you could make your solution faster. All of the FCC challenges can be solved without triggering the infinite loop protection feature, but some algorithms are more efficient than others.