For Loop stops after first iteration

Hi

New here :slightly_smiling_face:. I am trying to write a function to find the Smallest Common Multiple between a range of 2 numbers in an array. The multiple has to be divisible by all numbers in the range. I approached the problem as follows:

step 1: Create a new array and push all the numbers in range between the 2 numbers in the original array to this new array.

step 2: Define an upper bound for the multiples.

step 3: Loop through the multiples of the largest number, capping at the upper bound.

step 4: Loop through the numbers in the new array and divide the multiples from step 3 with each of the numbers in the new array.

step 5: If a multiple has no remainder after dividing by the new array numbers, return that multiple.

Problem: It seems that the 3rd or the 4th FOR loop in the following code stops after the first iteration. and it doesn’t even count the numbers outside of the original 2 numbers of the original array, although I’m looping through the new array. the console log from below code returns 10 instead of 2520.

This seems to be recurring problem for me on the FOR loop. any help will be much appreciated.

Code:

function smallestCommons(arr) {
  let newArr = []
  for (let i = arr[0]; i <= arr[1]; i++) {
    newArr.push(i)
  }

  let upperBound = 1
  for (let j = arr[0]; j <= arr[1]; j++) {
    upperBound *= j
  }
  for (let i = arr[1]; i <= upperBound; i += arr[1]) {
    for (let k = 0; k <= newArr.length; k++) {
      if (i % newArr[k] == 0) return i
    }
  }
}

console.log(smallestCommons([2, 10]))

What does this if statement mean? I don’t need you to tell me what all the symbols are, I’m looking for a human friendly explanation of this. I suspect you aren’t saying what you think that you are here.

Thank you for responding :slightly_smiling_face: Didn’t think I’d get any. This if statement divides the multiples by the numbers in the newArr, and if the divisions has no remainders, it returns the multiple (i). I hope that clarifies.

But what does the return statement do? Will any more code run after you return?

Well, the idea is that the smallest multiple (i) that is divisible by all the numbers in the array without a remainder is the final i. And when that happens, it’ll return that i. So it shouldn’t reach the return statement until it completes all iterations. And once it finds the correct i, the code shouldn’t run anymore.

If the return statement is inside of the loop, then it stops the loop iterations.

But if I take the return statement out of the loop, it throws an error, because it’s expecting and expression after the if statement. What am I missing?

Great question. I’d flip your logic around. When do you want to keep looking for the multiple?

If it doesn’t meet division rule. That the multiple must be divisible by all the numbers in the array with no remainder.

I changed the code to this, and now it’s creating an infinite loop at the “while”…

function smallestCommons(arr) {
  let newArr = []
  let stopLoop = false
  let multiple = 0
  for (let i = arr[0]; i <= arr[1]; i++) {
    newArr.push(i)
  }

  let upperBound = 1
  for (let j = arr[0]; j <= arr[1]; j++) {
    upperBound *= j
  }

  while (stopLoop === false) {
    multiple++

    for (let i = arr[1]; i <= upperBound; i += arr[1]) {
      for (let k = 0; k <= newArr.length; k++) {
        if (i % newArr[k] == 0) {
          stopLoop = true
        } else {
          stopLoop = false
          break
        }
      }
    }
  }
  return multiple
}

console.log(smallestCommons([1, 3]))

You have a few logic issues here.

First, why test i if you return multiple?

Second, break only breaks you out of the nearest loop.

You have the right rough idea, but the little details do your logic aren’t quite right.

O yeah, you’re right. I actually don’t need the upperbount anymore and can check i against the multiple. Let me play around a little more and see what I can come up with.

I really want to solve this with for loop, but it seems impossible! SMH!