Smallest Common Multiple working on console but not in freecodecamp website

Hello, please help!
I came up with a solution for the Smallest Common Multiple challenge that work when I try it locally on the browser console but when I try to run the test in challenge page it doesn’t get the last answer right… could it be because of the time it takes to calculate the answer? When running the code locally it doesn’t take long but is the only explanation I can think off. Please help.

Here is my code ( I added all the function calls at the end just for this post):

function smallestCommons(arr) {
  let numbers = [];  
  let multiple = 2;  
  let result =  0;

// function to put all the numbers in a new array
  let getArr = (g, l) => {
    for( let i = l; i <= g; i++){
      numbers.push(i);
    }
  };

// Just checking for the higher number
  if(arr[0] > arr[1]){
    getArr(arr[0],arr[1]);
  }else{
    getArr(arr[1],arr[0]);
  }

// reverse the array to start working with highest number
  numbers = numbers.reverse();

// run this until we get a result
  while(result === 0){

    let resultArr = [];

// divide the current multiple for each number
    for(let x = 0; x < numbers.length; x++ ){
      if(multiple % numbers[x] !== 0){
        // if the number is not valid don't continue to the smaller numbers
        break;
      }else{
        // if the number pass push it to a new array
        resultArr.push(0);
      }
    }
    
    // if all the number got passed to the new arr
    if(resultArr.length === numbers.length){
      //we got our multiple and we break the while loop
      result = multiple;
    }    
   
    //keep going searching in the loop
    multiple++;

  }

  console.log(result);
  return result;
}


smallestCommons([1, 5]); // should return a number.
smallestCommons([1, 5]); // should return 60.
smallestCommons([5, 1]); // should return 60.
smallestCommons([1, 13]); // should return 360360.
smallestCommons([23, 18]); // should return 6056820.

Thank you for your help!

Your browser information:

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

Link to the challenge:
https://www.freecodecamp.org/challenges/smallest-common-multiple

I’m not seeing where your function starts. Possibly you missed a few lines copying it from console? I was looking for something like function smallestCommons(arr) {

BTW nice to see someone commenting code especially when they’re asking for help.

Good luck on this!

Edit:
function smallestCommons(arr) {
missing very first line

I tested it and it works in codepen

Hi, you are right I missed the first line while posting it here, is fixed now; sorry for that.
Any idea why it doesn’t work on the challenge test?

I was hoping that was your problem. I tested your code so I know it works. I’m kind of stumped.

I remember one time I had to go into my browser developer console to delete the local storage for one algorithm scripting problem. I don’t remember exactly what the issue was but it reset that one problem page. When I was done I had to copy / paste my code back into the test window but my issues were gone.

Do you get any error message on that page? If a script runs long (half a second I think) the test shuts down assuming you have an infinite loop. Usually there is an error message above your code telling you what happened.

There is a support forum - that might get you results.

That’s all I got. I hope you get this figured out.

1 Like

I didn’t get any errors before. After your suggestion I refresh the page with Ctrl+ F5 and gave it a try.
Now I see the following in the console:

[Violation] ‘setTimeout’ handler took 1352ms

I guess I have to come up with a faster algorithm.
Thanks for the help!

There is a way to turn that off. It’s just a line of code you add to let the tester know you do not want that protection. I can’t remember what that one line is and I’m not at my computer right now

Thank you so much!
Set the first line of code to:

// noprotect

That fix it.
I will however try to come back and write more efficient code once I learn a little bit more.
Thank you again!