Tell us what’s happening:
I’ve tried to do a good job of explaining my thoughts by commenting in my code below.
My basic process is to generate each multiple of the largest number in the range and then test if that multiple is divisible without a remainder by each of the other numbers in the range. One I get to a number that is I return than number.
I’ve looked over my code and I feel like it should be working, but it clearly isn’t. I don’t think I have any syntax errors. I think the issue must be the order in which I’m calling things.
Would someone mind taking a look at it for me?
Your code so far
function smallestCommons(arr) {
let sorted = arr.sort();//sort to get them arranged lowest to highest.
let lcmArray = [];
//generate all number in the range with a for loop
for (let i = sorted[0]; i <= sorted[1]; i++) {
lcmArray.push(i);
}
//take a given array as a parameter
function lcmFinder(array) {
let test = false;
//set the highest number in the array
let current = array[array.length -1];
// use a for loop to generate each multiple of the highest number
for (let i = 1; test = false; i++) {
current *= i;
// creat an array to hold falsey and truthy values;
let x = [];
//run another for loop to test if each multiple of the highest number is divisble by all other numbers in the array.
for (let j = array[array.length - 2]; j >= array[0]; j--) {
if (current%array[j] != 0) {
//if the remainder doesn't equal 0 push false otherwise push false
x.push(false);
} else {
x.push(true);
}
}
// if x does not include any falsey values then set test to true to stop the first for loop and return the value of the current multipe of the largest number in the array.
if (!x.inludes(false)) {
test = true;
return current;
}
}
}
return lcmFinder(lcmArray);
}
smallestCommons([5, 1]);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36.
// if x does not include any falsey values then set test to true to stop the first for loop and return the value of the current multipe of the largest number in the array.
if (!x.inludes(false)) {
test = true;
return current;
}
Did you intend for this to be inside the for loop? It is currently after the loop.
“inludes” has a typo
The value false is not the same as a falsy value. Which are you looking for?
for (let i = 1; test = false; i++) {
= is an assignment operator, not a comparison operator.
After this I believe that you’ll still find that you are getting incorrect values, but hopefully that should help you move forward on your debugging.
That was very helpful and exactly the kind of assistance I was looking for. I corrected the issue with the assignment operator because I did intend for it to be a comparison operator, but now I’m getting a warning about an infinite loop Do you think I’m going about this process incorrectly?
The infinite loop warning is because of the amount of time your code takes to run. To protect your from crashing your browser with an infinite loop. Now that you have a working solution, you can try to make it more efficient.
For this sort of mathy challenge, it’s perfectly valid to look up how to solve the problem mathematically/logically and then figure out how to turn that abstract approach into code.
If you want to look at formulae related to this challenge, the Wikipedia article is good.
I have the while loop to increment to the next multiple of the largest number in the range. The loop variable serves to give it an ending and prevent an infinite loop. I wasn’t sure how else to solve the infinite loop warning.