Javascript: Use reduce() method to find the LCM in an array -


i trying find lcm(least common multiples) among several numbers in array. lcm between each 2 numbers in array, use reduce() method not working.please tell me what's wrong? thanks.

function gcd(a,b){   //gcd: greatest common divisor   //use euclidean algorithm   var temp = 0;   while(a !== 0){     temp = a;     = b % a;     b = temp;    }   return b; }  function lcm(a,b){   //least common multiple between 2 numbers   return (a * b / gcd(a,b)); }  function smallestcommons(arr) {   //this function not working, why?   arr.reduce(function(a, b){     return lcm(a, b);   });  }  smallestcommons([1,2,3,4,5]); //------>undefined 

your smallestcommons function missing return. undefined default return value functions don't have explicit return.

function smallestcommons(arr) {   return arr.reduce(lcm); } 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -