Hi guys,
anyone interested in helping me reduce the amount of interactions these lame lines of code is doing?
the last test would not pass as the fcc is stopping the runtime to protect us from running infinite loops.
Here is the code:
function smallestCommons(arr) {
//find if arr is in order.. and create range
let range = [];
function orderAsc() {
for (var i = arr[0]; i <= arr[1]; i++) {
range.push(i);
}
}
function orderDes() {
for (var i = arr[0]; i >= arr[1]; i--) {
range.unshift(i);
}
}
(arr[0] < arr[1]) ? orderAsc() : orderDes() ;
let ans;
let rangeNum = range.length;
let divisor = range[rangeNum - 1];
// checks whether an element will not return zero
let isZero = function(element) {
return element !== 0;
};
let test = [1]
for (var i = range[rangeNum - 1]; test.some(isZero); i += divisor) {
test = [];
range.forEach(function(x) {
test.push(i % x);
});
ans = i;
}
return ans;
}
smallestCommons([1, 5]); // should return 60.
smallestCommons([5, 1]); // should return 60.
smallestCommons([2, 10]); // should return 2520.
smallestCommons([1, 13]); // should return 360360.
smallestCommons([23, 18]); // should return 6056820.