Tell us what’s happening:
Describe your issue in detail here.
Not sure why this code isn’t working. I created the helper function to find the smallest multiple to then be applied to the parent function passing in the array’s arguments…but it’s still only giving me the highest number in the passed-in array. It doesn’t seem to be iterating in the parent function at all…or that’s what it looks like. I have tried it with the helper function inside and outside the parent function, and both times it is giving me the same answers printed to the console. I checked for typos and didn’t see any…unless I somehow missed one, but maybe the loop in the parent function isn’t working for some reason? I don’t know why it’s not giving me 60 from [1, 5].
Your code so far
function smallestCommons(arr) {
// sort the array
arr.sort((a, b) => a - b);
// create a helper function that will find the smallest multiple between two given numbers
function findSmallest(a, b) {
// apply Math.min and Math.max to ensure that you're getting the absolute minimum and maximum numbers of those being passed in and initialize them to a variable
let low = Math.min(a, b);
let high = Math.max(a, b);
// create a for loop that will follow the common multiple pattern and return "i"if the low number and high numbers are both evenly divisible
for (let i = high; i <= high * low; i + high) {
if (i % low === 0) {
return i;
}
}
}
// console.log(findSmallest(...arr));
// do the same as in the helper function to test the multiples, but this time passing in the arguments of the given array
let newLow = Math.min(...arr);
let newHigh = Math.max(...arr);
// initialize a function call applied to the arguments of given array through the previously created helper function to a new variable
let smallest = findSmallest(...arr);
// create a similar for loop to the one in the helper function, but this time set to the new variables that were initialized to the passed in array's arguments
for (let j = newLow; j <= newHigh; j++) {
// create conditional logic that tests if the current "index" in the interative process is not evenly divisible by the "smallest" variable
if (smallest % j !== 0) {
// if this is truthy, then set "smallest" equal to a helper function call on the current "smallest" and current "index"
smallest = findSmallest(smallest, j);
}
console.log(newLow);
console.log(newHigh);
console.log(smallest);
// return "smallest"
return smallest;
}
// return "smallest"
return smallest;
}
console.log(smallestCommons([1,5]));
// console.log(smallestCommons([2,10]));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/112.0
Challenge: Intermediate Algorithm Scripting - Smallest Common Multiple
Link to the challenge:
The first thing that jumps out at me. The for
loop at the end, how many times is that loop going to execute?
j <= newHigh
that should be the ending point…would it not stop when it hits the “newHigh” variable?? Since it’s less than or equal to that number and increasing the count at the end instead of decreasing…the console didn’t tell me it would create an infinite loop, so that end point should be working?? Unless I’m missing something…
What is the very last statement at the end of that for
loop? What does that do?
it should be the equivalent of “newLow + 1”
so then 1 would be added to newLow until it hits newHigh and then the loop should stop looping and return “smallest”…
You didn’t quite answer my question. Literally, what is the last statement in the for
loop? In other words, what is the code? What does that code do? Based on that answer, how many times will the for
loop execute?
well, it’s going to execute until it hits the conditional statement being truthy right?
smallest % j !== 0
if the conditional statement is truthy, then it’s going to run the helper function on the “smallest” and the current iteration of the loop
going through the helper function, it’s supposed to check if it’s a multiple and if not, add another “high” variable to fun through that function until it finds the smallest common multiple and then return the answer to the parent function
…am I on the right track, or am I still missing the point?
OK, maybe I am not asking the question clearly? So I will answer my own question (at least part way). The last statement in your for
loop is:
return smallest;
What does this do? Based on that, how many times is the for
loop ever going to execute?
ohhhh…it’s only ever going to return the initial “smallest” value?
so I gotta return “smallest” inside the if statement…
it keeps giving me 5, because if I return it there, it will exit the function at the first iteration
Now that is not the only problem here. I’m just helping you debug some of the obvious issues.
Second question, have you tested your help function findSmallest
to make sure it is doing what you actually want it to do? For example, if you called it as
findSmallest(3,5)
What number should it return? And does it return that number?
By mental math, that should give me 15, but when I checked it in the console just now, it gave me an error message:
Potential infinite loop detected on line 12. Tests may fail if this is not changed.
for reference: (line 12) :
for (let i = high; i <= high * low; i + high) {
should it be changed to:
for (let i = high; i <= high * low; i += high) {
??? to increment and assign at the same time??
hahaha…that fixed that issue
…something still isn’t working though. The helper function is working and the return statement has been relocated, but it’s still not iterating somewhere…
Ya, you’ve got some logic to work out. The challenge is asking you to find the smallest number that can be divided evenly by every number in a range of numbers. So if you are passed in [3, 7]
then you need to find the smallest number that can be evenly divided by 3
, and 4
, and 5
, and 6
, and 7
. Where are you doing this check?
oh, I thought the second loop was constantly checking that through the incrementing iteration…if it isn’t then…lol…I’ll have to see what I can do.
Though I don’t think it’s causing it’s causing an error is your check for smallest % j !== 0
even needed? If you just called findSmallest (smallest, j) and smallest was divisible by j, what would the function return?
Well, I checked the console.log on the function call after removing the “if” statement, and I got 5 and 10 respectively after passing in the arrays: [1,5] and [2,10]…but maybe that’s on the right track…I may be able to fix that and run it through the range to get the lcm…idk, I’ll try it though.
By the way, I figured it out…I re-worked the problem a different way. Instead of finding the Greatest Common Divisor, I did it finding the Highest Common Factor (HCF). I made 3 helper functions and then reduced it based on the function I created for the Least Common Multiple, dividing it by the HCF. The function was lengthy, but it worked.