Tell us what’s happening:
I’m having trouble with my loop. What I’m trying to do:
loop through the range of numbers between the parameters and divide each one against the largest parameter (separate variable created called “counter”)
If the result is NOT an integer, add a number to the counter and restart the loop.
If every number in the loop is divided into the counter and returns an integer, then return the “counter”
However this isn’t working as I have written it. Does “continue” not restart a loop? How do you otherwise restart the loop again with the new counter (counter = counter + 1) in place?
Thanks for any help or any other ideas on my approach.
Your code so far
function wholeNumberCheck(num) {
return Number.isInteger(num);
}
function smallestCommons(arr) {
var highestNum = Math.max(...arr)
var lowestNum = Math.min(...arr)
var counter = highestNum;
for (var i = lowestNum; i <= highestNum; i++){
if (wholeNumberCheck(counter/i) === false){counter = counter + 1; continue}
}
return counter;
}
console.log(smallestCommons([2, 10]));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36.
Continue won’t be useful here because it does not terminate the execution of the for loop. When your if statement evaluates to false, you want to restart at i == lowestNum with the new counter variable. Break may be a more appropriate. You can read about break and continue here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/continue
However, your code won’t work if you just change the continue to a break. At the moment your return statement triggers before you’ve found the lowest common denominator. Think about how to restart your for loop each time a number isn’t evenly divisible by counter…there are a few ways to do this.
Hope that helps feel free to ask more questions if you are stuck.
Thanks for the feedback. Unfortunately I’m still stuck, can you give me a hint on how you would go about restarting the loop again each time a number isn’t evenly divisible by counter?
Thank you! I was able to get it working. I will try recursion next time as a solution.
Question: Even though this test passed, I still got a lot of “Potential infinite loop” warnings, is this standard?
function wholeNumberCheck(num) {
return Number.isInteger(num);
}
function lowestNumCheck(num) {
if (num === 1){return 2} else {return num}
}
function smallestCommons(arr) {
var highestNum = Math.max(...arr)
var lowestNum = lowestNumCheck(Math.min(...arr))
var counter = highestNum;
var lowestCommon = 0;
while (lowestCommon != highestNum){
for (var i = lowestNum; i <= highestNum; i++){
lowestCommon = lowestCommon+1;
if (wholeNumberCheck(counter/i) === false){counter = counter + 1;}
}
}
return counter;
}
console.log(smallestCommons([2, 13]));
Mmmm when I was playing around with your solution I tried while (lowestCommon != higestNum) and had the same error messages. I found this one worked well:
function wholeNumberCheck(num) {
return Number.isInteger(num);
}
function smallestCommons(arr) {
var highestNum = Math.max(...arr)
var lowestNum = Math.min(...arr)
var counter = highestNum;
var lowestCommon = 0;
while (lowestCommon == 0) {
for (var i = lowestNum; i <= highestNum; i++) {
if (wholeNumberCheck(counter/i) === false) {
break;
}
if (i == highestNum) {
/*This if statement will only be executed when the
first statement hasn't been triggered (i.e. when all
numbers between lowestNum and highestNum are
evenly divisible by i), so we can assign the value to
lowestCommon here. */
lowestCommon = counter;
}
}
/*increasing the value of counter outside of the for
loop but inside the while loop, so the for loop will
restart with the new value of counter until the correct
value of counter is found and assigned to lowestCommon. */
counter += highestNum;
}
return lowestCommon;
}
console.log(smallestCommons([1, 5]));