As usual, I only check the hints button after a solution is found. Then we proceed to investigate more effective/interesting ones posted there.
There was is one elegant answer by @JanEgbers that I can’t ignore. It’s way more compact then my litte monster, and so far I couldn’t understand how it actually works.
Anyways, there it goes:
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;
}
My little monster:
function smallestCommons(arr) {
let a;
let b;
let sequence = [];
let multiplier = 1;
//put smaller element at 'a' and bigger at 'b'.
if (arr[0] > arr[1])
{a = arr[1]; b = arr[0];} else
{a = arr[0]; b = arr[1];}
//make an full sequence array
for (let c = a; c <= b; c++) {
sequence.push(c);
}
//check if n is a multiple of everyone in the sequence array
let answerFound = (n) => {
return sequence.map(e => n % e == 0)
.every(e => e)
}
//immediately-invoked-recursive function multiplies
//the biggest number until an answer is found.
return (function recAnswer(answ, mul){
return answerFound(answ*mul) ? answ*mul :
recAnswer(answ, mul+1);
})(b, multiplier);
}
//test below
console.log(smallestCommons([2,5]));
Maybe I need more coffee, already late…