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