javascript - Smallest Common Multiple Code produces the wrong answer on one array -
[edit]
i should clarify, attempting find smallest common multiple of range of numbers. sorry that. have attempted solution still run incorrect answer on last array [23, 18].
function smallestcommons(arr) { arr = arr.sort(function (a, b) { return - b; }); var count = 1; (var = arr[0]; <= arr[1]; i++) { if (count % !== 0) { = arr[0]; count++; } } return count; } smallestcommons([23,18]);
my solution produces 2018940 when should 6056820
your endless loop becouse of inner loop starts @ value 19 , runs 22
414 (smallestmultiple of 18 & 23) % 19 == 15 414 % 20 = 14 414 % 21 = 15 414 % 22 = 18
which leads statement if(count % == 0) being false , loop goes on 415 416 ...
if u want least common multiple
var issmallestmultipe = 0; while(issmallestmultiple == 0) { for(var = 1; <= arr[1]; i+) { if((arr[0]*i) % arr[1] == 0) { issmallestmultiple = arr[0] * i; } } }
Comments
Post a Comment