My code is running fine on chrome editor tool but not on the freeCodeCamp site

function smallestCommons(arr) {
  let init = Math.max(...arr);
  let flag = 0;
  while(init){
    flag = 0
    for(let i=Math.min(...arr); i<=Math.max(...arr); i++){
      
     if(init%i != 0){
       flag++;
       break;
     }
     }
     if(flag === 0){
       break; 
    }
    init++;
  }
  return init;
}


console.log(smallestCommons([23,18]));

Can you please describe the issue you are having in words and provide a link to the challenge you are working on? I’d assume that your solution has a problem with the infinite loop protection at the very least because this is an inefficient approach.

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 it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

The challenge is here. In the future, please use the Get Help -> Ask for help and this will be provided automatically.

When I run your code on FCC, it passes.

If it is the last test(s) that is failing, it may mean that your code is inefficient and the FCC test is timing out. It may “work” just fine on other platforms but not on FCC.

1 Like

It should work if you remove the console.log. It does for me anyway.

Thanks to everyone for replying. The code has no errors or infinite loop problem as I reloaded the site and pasted my code. The Code passed all the tests I think it was a storage problem or something like that idk

Your code is pretty slow, so it could have just taken too long on the particular browser you used.

yup I got that please tell if you got any suggestion on speeding up the existing code

Looking at the code, some to consider …

Math.max(...arr);

You are running this in two different places. The second one is in a loop inside a loop (being evaluated on each iteration. Rather than recalculate that every time, why not store that in a variable.


    init++;

Do we want to just increment by one. I mean, just like you knew to start the count with the largest element in the range. Won’t all common multiples be at least a multiple of that?


I don’t know, the looping logic looks a little odd to me - I’ll have to take a closer look later. I may be wrong, it just looks odd.

1 Like

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