Smallest Common Multiple

We would have to tell it to show us numbers. We would have to create the pattern for 5 to follow. Right?

What does show mean? I really am not sure how a number can show numbers?

Show as in show us all the multiples of 5. Which would involve adding 5 by it’s own value over and over

I don’t think I am answering this how you want me to though. I promise I’m trying :melting_face:

Maybe we can tell it to increment by 5, instead of the common i++ you would do i += 5…but the max value is not always 5 so you could say max instead?

To me, show means somehow print out to the console. Is that what you want?

No that is not what I want, that wouldn’t help

That’s why I was confused.

I think you have the right rough idea?

Ahhhh I see, completely understand, sorry about that. I will work on making a solid attempt at solving this using for and if now. Then I’ll come back with what I have

function smallestCommons(arr) {

arr = arr.sort((a,b) => a - b);
let [min, max] = arr;
let multiple = max;

for (let i = min; i < max; i++) {
  if (multiple % i !== 0) {
    multiple += max; 
    i = min - 1; 
  } 
    else if(i == max) {
      return multiple; 
    }
    return multiple;
  }

smallestCommons([1,5]);

Can you see where I went wrong?

function smallestCommons(arr) {

arr = arr.sort((a,b) => a - b);
let [min, max] = arr;
let multiple = max;

for (let i = min; i < max; i++) {
  if (multiple % i !== 0) {
    multiple += max; 
    i = min - 1; 
  } 
    else if(i === max) {
      return multiple; 
    }
  }
  return multiple;
}

console.log(smallestCommons([1,5]));

Nevermind, my return was in the wrong spot :sweat_smile:

This is a bad idea. Changing the loop iteration variable inside of a for loop gets confusing.

It works, but I would try to express this logic in a way that doesn’t involve manually resetting i. You are masking the fact that really this is a nested loop.

Man I thought I had it, every line makes sense to me :sob:

But if you say changing the loop iteration variable gets confusing, then I definitely believe you. I can’t think of another way to do it that makes sense to me

Normally, when you create a for loop, the 3rd part of the definition is the only part that modifies the initialized variable (first part). You could instead use a while loop that allows you to modify i in different parts of the code.

Also, since you end up returning multiple if the loop ends or if i is equal to max, you could make your while loop condition the following and get rid of the else if part of the code.

while (i < max && i !== max) {

Personally, I would change the variable named i to be num to make it more readable throughout the function.