I would start by writing out in your own words the steps needed to create this algorithm.
function for number Collection in array
-Find minimum and maximum number in the array.
-Create a list of numbers ranging between them. Square brackets [a , b]. with a being min and b max.
function for LCM
-let lcm = 1
-let arr1 = array from the function where we collected an array from range
-start a loop ranging from 2 to the max number in the array. let it be i.
-start a loop again on the inclusive range of array. let it be j
if j % 2 === 0
then lcm = lcm * 2;
-arr1[j] = arr1[j] / i
Important note: here we are calculating LCM more times it is required. A method here is needed where I can straight away divide all the elements which are % i === 0.
This isnât really a list of steps. Youâre describing the code that you know is wrong in words. Start with the algorithm that you know is good and try to condense it into steps. If you were going to tell a student how to do this by hand with pencil and paper, what would they do?
Youâre going to need a while loop as your second loop, I think, but why isnât clear unless you understand the algorithm.
Currently, you have your while loop for dividing out factors inside of the loop over the range, which means that you are multiplying the 2 in your example above 3 times instead of twice.
Ok I was able to implement⌠something. I followed pseudocode that I described above.
Itâs not looking good:
three nested loops
plus some usage of âlooping methodsâ: some() - used twice in the code, reduce() - used once in the code
To do this by hand you would keep track of these numbers i and j? I would not.
The biggest issue in your code is tow big discrepancies between the pencil and paper algorithm and your loops.
The pencil and paper version goes across a row, but you arenât doing that. You are removing all factors of 2 from each number in the range instead of removing the first factor of 2 from all numbers in the range and then removing the second factor of two from all numbers in the range, and so on until you run out of factors of two.
Omg Omg Omg I did it finally. Thanks for the some(), I think I solved by that method. I am so happy!!! I will share my code in the message with you. I think we canât share the code publicly here.