Result mismatch even for same inputs

Tell us what’s happening:
Kind of old-stuff of asking question, but this is true! The code is giving exactly same o/p as mentioned in test-cases in my terminal, but not passing last two test-cases in FreeCodeCamp’

Your code so far



function smallestCommons(arr) {
	var first = -1;
	var last = -1;
	
	if(arr[0] > arr[1]){
		first = arr[1];
		last = arr[0];
	}
	else{
		first = arr[0];
		last = arr[1];
	}
	
	var temp = [];
	for(var i=first; i<last+1; i++){
		temp.push(i);
	}
	
	for(var i = 2; i<Number.MAX_SAFE_INTEGER; i++){
		var flag = true;
		for(var j=0; j<temp.length; j++){
			if(i % temp[j] != 0){
				flag = false;
				break;
			}
		}
		if(flag === true)
			return i;
	}
}


smallestCommons([1,5]);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/smallest-common-multiple/

My first guess without looking into your code that your function runs out of time before computation of a correct result. In your browsers console no timeout is set - so it works. The goal now is to refactor to make a function more efficient