Bonfire 32: Smallest Common Multiple

TASK: Find the smallest number that is evenly divisible by all numbers in the provided range.

Maths can be daunting;

function smallestCommons(arr) {

// define which value is greater
var min = arr[0];
var max = arr[1];

if (arr[0]>=arr[1]) {
min = arr[1];
max= arr[0]

// create array with all values within the range

function range(min, max) {
var arr = [];
for (var i = min; i <= max; i++) {
return arr;

// greatest common denominator formula
function gcd(a, b) {
return !b ? a : gcd(b, a % b);

// least common multiple formula
function lcm(a, b) {
return (a * b) / gcd(a, b);

// calculate for all values in the range
var multiple = min;
range(min, max).forEach(function(n) {
multiple = lcm(multiple, n);

return multiple;


Bonfire 32: Smallest Common Multiple

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s