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

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -