Intermediate Algorithm Scripting - Smallest Common Multiple

Tell us what’s happening:

I want to retest this for loop until all 5 values are true.
My thought process is to create a while loop that is based off a variable “go” that is initialized to false. As long as go is false, then i want to continue to perform my loop.

I set an if statement to see if the answer % the array number has no remainder, then the go value is set to true. If that is not the case, then I want to exit the loop with go as a false value.

From there I want to increment the answer var by one and retest the loop with the new answer value. I want to continue this cycle until all 5 numbers in the array pass the boolean.

I do not understand why the while loop will not continue to run the for loop until all numbers of the array pass the boolean with a true value (incrementing the answer by one of course)

Your code so far

function smallestCommons(arr) {
  let go = false;
  let rangeArr = [];

  // let answer = 1; let go = false;
  arr.sort((a, b) => a - b);
  let answer = arr[1];
  for (let i = arr[0]; i <= arr[1]; i++) {
    rangeArr.push(i);
  }
  // I want to continue to increment the answer
  // until "if (answer % num === 0)" is true for every number in the rangeArr
  // 
  while (!go) {
    for (let j = 0; j < rangeArr.length; j++) {
      if (answer % rangeArr[j] === 0) {
        go = true
      } else {

       return go = false;
      }
    }
    answer++;
    console.log(answer)
  }


  // return go;
  // answer += 1;
 
  // if ALL the arrNum modulus answer === 0 then its the num,
  // otherwise increment the num and try again
  console.log(rangeArr);

  return answer;
}

smallestCommons([1, 5]);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36

Challenge Information:

Intermediate Algorithm Scripting - Smallest Common Multiple

For starters, this doesn’t seem right. What does a return statement do?

honestly at that point I wasn’t sure, so I tried to make it stop the loop so it would not go to the next number in the array and turn the value of go to true. How could I ensure that if it does not pass the boolean that it exits the current for loop, increments the answer, then runs the for loop again with the updated value of answer?

but a return statement will stop executing the code and return the value from the loop?

Correct, so you probably don’t want one inside the for loop :slightly_smiling_face:

1 Like

thank you, i will make that change. However, I am still confused as to preventing the loop from reading the next number and turning the go variable to true before I am able to increment the answer variable and retest the numbers of the array. Am I on the right track by putting this for loop inside of a while loop to continue to retest the for loop, or do I need a different approach?

A for loop inside of a while loop is fine (that’s how I just solved it). I think you are having trouble with how to break out of the for loop early. I just gave you a big hint. Do you see it?

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.