Smallest Common Multiple - right solution but doesn't pass test

Smallest Common Multiple - right solution but doesn't pass test
0

#1

Tell us what’s happening:
Hi everyone!
Will you, please, help me to understand why my solution doesn’t pass the test for the values [1, 13]? I get the right answer 360360 on Codepen. It passes all the other tests.
Thank you very much!

Your code so far
Here is a link to my solution on Codepen Smallest Common Multiple

function smallestCommons(arr) {
    let min = Math.min(...arr); //min element of 'arr'
    let max = Math.max(...arr); //max element of 'arr'
    let array = [];     //array to store all the values between 'min' and 'max' included
    let minComMult = 0; // declare smallest common multiple variable

    for (let j = min; j <= max; j++) {  // create an array of all numbers between 'min' and 'max' ('min' and 'max' included)
        array.push(j)
    }

/*for debugging purposes (IMPORTANT: runs slow with large numbers)
    console.log('min = ', min, ' max = ', max); 
    console.log('array = ', array);
*/

    

    const product = array.reduce((total, value) => {    //Find the product of all the numbers in the array
        return total *= value                           //in case this is the smallest common multiple (SCM)
    }, 1)

    //console.log('product = ', product)

    for (let i = 1; i <= product; i++) {                //let 'i' be a multiple ranging from one to 'product'
        
       // console.log(' i = ', i);

        if (                                            //if the product of 'i' and 'min' is divisible by every element in the 'array' (true)
            array.every(function(isFactorOf) {
              //  console.log('modulus = ', (min *i) % isFactorOf);
                return (min * i) % isFactorOf == 0
            })
        ) {
            minComMult = min * i;   //smallest common multiple is here!!!
            return minComMult
        }
    }
    return minComMult;
  }
  
  
  console.log(smallestCommons([1,13]));

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/smallest-common-multiple


#2

I just cut and paste your code into the fcc challenge, and it passed.


#3

@kevinSmith Thank you so much. I feel myself kind of stupid now… :slight_smile: Everything solved when I removed console.log on the last line, when there is a function. I lost quite a lot of time trying to figure out what is wrong and just missed that the last line was different.