# Smallest Common Multiple not working for integer 4

Smallest Common Multiple not working for integer 4
0
#1

Tell us what’s happening:

There is a bug in the logic of my code, why is it not working for the integer 4 when the integer 4 is part of the range? and it seems to be doing what I want for all the other integers. Why is this?
work done so far on codepen

To finish this problem, I will attempt to use the method as explained in the end of this video

how to turn the above video into pseduocode?
Attempt: any unique prime element found must be included in the product. if a mini array contains a repeated prime, then we need to use it in the product corresponding to the highest number of times it appears in any mini array.

so what I would like to do is to start pushing each element from each mini primes array into a larger array, and if there is an element encountered which was already pushed, we need to check the current miniprime array if that same element appears again, and if it does we keep pushing it, -1 to take into account the element that was already pushed. Then just reduce the array multiplying all the values together and return that…

so basically, why is my code not working for the integer 4, and how can I describe the last step better?

``````
function smallestCommons(arr) {
return arr;
}

smallestCommons([1,5]);
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36`.

0 Likes

#2

I see no one helped, but here is the solution in case it helps someone else:
The bug in the below code snippet from the codepen is caused by checking if n is equal to two and then saying while n is greater than 2. In order to fix this delete the first in the below code and change the statement checking if n is greater than 2 to instead check if n is greater than or equal to two.

`````` if(n===2){
factors.push(2);
}
while(n>2){
if(n % divisor == 0){
factors.push(divisor);
n= n/ divisor;
}
``````