Smallest Common Multiple - Infinite loop escape?

Smallest Common Multiple - Infinite loop escape?
0

#1

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

``````
// 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]);
``````

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

#2

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

#3

You have an infinite loop.

The following line is the culprit:

``````for (let j = maximum; j = true; j++) {
``````

The condition j = true will always be true, so it will just keep looping forever. Can you explain what you thought you were doing in this line?

You also have another issue down later in your code:

``````if (i=range.length) {
``````

The above is not a comparison, but is an assignment. i will always be a number greater than 0, so it will evaluate to true.

#4

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.

#5

Where ever you write return SMC, it will kick out of the function if that line executes. The problem your current algorithm does not allow that line to execute.

I suggest you write out your algorithm in plain language first, before attempting to write code. I can not see any logic in what you have write below the var SMC line of code.

If you can explain what your are trying to accomplish in everything below var SMC, then we can guide you to something. Right now, you seem to not understand to calculate SMC.

#6

Alright, thanks, will do that

#7
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?

#8

You need to break this step down much more. Because it contains the logic.

#9

How will you know when you have found the SMC?

#10

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

#11

I you were given just two numbers 3 and 8, explain your process of calculating the SMC for just those two numbers and not the SMC for the entire range (3, 4, 5, 6, 7, 8)?

Once you figure out the process for two numbers, it is very simple to solve for a range of numbers.

#12

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

#13

Your code attempt tells me you have no idea how to calculate SMC.

Again, try to explain to me how you would calculate the SMC for just two number (3 and 8) on paper. Do you know how to calculate the SMC for 3 and 8 on paper? If so, what is the answer and what steps did you take to arrive at that answer?

If you can not write the steps, you are not going to be able to write the code for any steps.

#14

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?)

#15

Just multiplying the two numbers is not how you calculate the SMC for any given two numbers. It is a little more involved than that. For example, the SMC of 8 and 36 is not 288.

Use your favorite search engine and research how to manually calculate the SMC for 8 and 36.

#16

Randell, what is the application of all these complex (atleast I think so ) 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?

#17

Solving these types of algorithms help you to develop better problem solving skills for when you encounter a difficult problem in any type of development situation. If you asked me to write a program to calculate the optimal angle of entry for the space shuttle given a certain speed to avoid burning while re-entering the atmosphere, I would not know how to do that off the top of my head. However, with a little bit of research and brushing up on some math and physics topics, I could probably do it.

Sometimes, you have to do some outside research for a subject you are not familiar with. That is just part of it.

#18

Alright, thanks for the help today