JavaScript -Find the smallest common multiple of a provided parameters



Find the smallest common multiple of the provided parameters that can be evenly divided by both, as well as by all sequential numbers in the range between these parameters.



  • The range will be an array of two numbers that will not necessarily be in numerical order.

  • For example, if given 1 and 3, find the smallest common multiple of both 1 and 3 that is also evenly divisible by all numbers between 1 and 3. The answer here would be 6.



Solution Basic approach


function smallestCommons(arr) {

    var max = Math.max(arr[0], arr[1]);
    var min = Math.min(arr[0], arr[1]);
    var mltple = max;

    for (var i = max; i >= min; i--) {

        if (mltple % i !== 0) {
            mltple += max;
            i = max;
        }
    }

    return mltple;
}

console.log(smallestCommons([1, 5]));

Output:


60

download android app

Popular javascript Examples









codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube