Smallest Common Multiple - Infinite loop escape?

Tell us what’s happening:
Hey, I thought the // noprotect comment in the beginning of the file made the code run to the computing-limits (?), rather than, for security purposes, breaking the code execution early.

My quesitons are:

  1. Can I escape these security boundaries?
  2. How can the solution be improved

Thanks

Your code so far


// noprotect
  function smallestCommons(arr) {
var maximum=Math.max(...arr); //Set maximum of two values passed into function to "maximum"
var minimum=Math.min(...arr); //Set minimum of two values passed into function to "minimum"
var range=[]; //Initialize array for later use

for (let i=minimum;i<=maximum; i++) {
range.push(i);
} //Push every number in the range between the two numbers passed into the function to the array "range"

var SMC; //Assign smallest common multiple to this variable when it's later found

/*SMC=Evenly divided by both, as well as by all sequential numbers in the range between these parameters.*/

for (let j=maximum;j=true;j++) {
for (let i=0; i<range.length;i++){
if(i%j===0){
continue;
} 
if (i===range.length) {
  return SMC=j;
j=false;
}
break;
}
}
}


smallestCommons([8,5]);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0.

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

I think (but I’m not sure) that the // noprotect flag does not exist in the new curriculum.

1 Like

I don’t know how to reference the result of the if statement that I later introduce. I want the loop to stop running when the SMC is found.

Alright, thanks, will do that

  1. Start at “maximum” of input “arr”, (8 in my example) increment from start by 1. (I’ll use “i” to reference “start” + all prev. incrementations later.)
    1.1 Above should be done until SMC is found.
    2 Find SMC for every element of array “range”.
    2.1 If there’s no rest in the division between “i” and every element in range array. Then return SMC

Have a hard time reducing it to english further. Hope you can understand it, but do you have any resources for SMC?

If there’s no rest in the division between “i” and every element in range. Then, that would be the SMC.
…is my thinking

I don’t think this is it, but it’s all I can think of, would appreciate a hint for how the other solution works:

var SMC = function (arr) {

for (let i=arr[1];i=true;i++){ //Referencing arr[1] becuase it's better to start there than at 0
var hodl=arr[0]%i;
var hold=arr[1]%i;
if(hodl===0 && hold===0){
return i;
}
}
}

SMC([5,8]);

//Possible infinite loop

I could do 3x8==24. That is how I would solve it on paper. I don’t know any general algorithm for it, except for multiplying the two arguments to the function… (Would that even work in every case?)

Randell, what is the application of all these complex (atleast I think so :joy: ) math problems? Are they for learning to write english algos and turn them into code or do you actually use them often when building websites for example?

Alright, thanks for the help today