function smallestCommons(arr) {
//determine which of two numbers is the largest and smallest
var min = Math.min(...arr);
var max = Math.max(...arr);
// create array of all numbers inclusive of max and mins, i.e. the ends
var fullArray = Array.from([min, max], function(a, b) {return min + b});
//sort the array of all numbers inclusive of max and min, i.e. the ends
var sortedFullArray = fullArray.sort((a, b) => b - a);
//function: greatest comomon divisory via Euclid's method for any two numbers
function gCD(a, b) {
var temp = 0;
if (b === 0) {
return a;
}
else if (a === 0) {
return b;
}
else {
while (!b) {
temp = b;
b = a % b;
a = b;
}
}
}
//function: least common multiple by applying Euclid's method for any two numbers
function lCM(a, b) {
(a * b / gCD(a, b));
}
//main function that iterates through entire array of numbers to find LCM for all
function lCMIterate (arr) {
//make a copy of array passed in for use in function
var arrCopy = [].concat(arr);
var lCMOutput = 0;
lCMOutput = lCM(arrCopy[0], arrCopy[1]);
arrCopy.shift;
arrCopy.shift;
arrCopy.unshift(lCMOutput);
if (arrCopy.length === 2) {
return lcmoutput;
}
else {
while (arrCopy.length > 2) {
lCMIterate(arrCopy[0], arrCopy[1]);
}
}
}
lCMIterate(sortedFullArray);
}
smallestCommons([1,5]);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0.
I don’t have the time to debug it throughly but I see you are missing return statement. Where is the return statement of your smallestCommons function? if it hasn’t one it just returns undefined
Thank you still having trouble. I updated code to another thread. Sorry, first time poster and new to this. Haven’t had any issues solving challenges until this problem!!!
Tell us what’s happening:
Too much recursion error or too many resources or possible infinite loop. Struggling any help would be great.
Your code so far
function smallestCommons(arr) {
//determine which of two numbers is the largest and smallest
var min = Math.min(arr[0], arr[1]);
var max = Math.max(arr[0], arr[1]);
//create an array with all the numbers inclusive of largest and smallest
var fullArray = [ ];
for (var i = min; i <= max; i++) {
fullArray.push(i);
}
//sort the array of all numbers inclusive of max and min, i.e. the ends
var sortedFullArray = fullArray.sort((a, b) => b - a);
//function: greatest comomon divisory via Euclid's method for any two numbers
function gCD(a, b) {
var c = 0;
while (b != 0) {
c = b;
b = a % b;
a = c;
}
return a;
}
//function: least common multiple by applying Euclid's method for any two numbers
function lCM(a, b) {
return (a * b / gCD(a, b));
}
//recursive function that iterates through entire array of numbers to find LCM for all
var lCMOutput = 0;
function lCMIterate (arr) {
//try first two numbers in array for
lCMOutput = lCM(arr[0], arr[1]);
arr.shift;
arr.shift;
arr.unshift(lCMOutput);
if (arr.length === 2) {
return lCMOutput;
}
else {
lCMIterate(arr);
}
}
lCMIterate(sortedFullArray);
return lCMOutput;
}
function smallestCommons(arr) {
//determine which of two numbers is the largest and smallest
var min = Math.min(arr[0], arr[1]);
var max = Math.max(arr[0], arr[1]);
//create an array with all the numbers inclusive of largest and smallest
var fullArray = [];
for (var i = min; i <= max; i++) {
fullArray.push(i);
}
//sort the array of all numbers inclusive of max and min, i.e. the ends
var sortedFullArray = fullArray.sort((a, b) => b - a);
//function: greatest comomon divisory via Euclid's method for any two numbers
function gCD(a, b) {
var c = 0;
while (b != 0) {
c = b;
b = a % b;
a = c;
}
return a;
}
//function: least common multiple by applying Euclid's method for any two numbers
function lCM(a, b) {
return (a * b / gCD(a, b));
}
//recursive function that iterates through entire array of numbers to find LCM for all
var lCMOutput = 0;
function lCMIterate (arr) {
//try first two numbers in array for
lCMOutput = lCM(arr[0], arr[1]);
arr.shift;
arr.shift;
arr.unshift(lCMOutput);
if (arr.length === 2) {
return lCMOutput;
}
else {
lCMIterate(arr);
}
}
lCMIterate(sortedFullArray);
return lCMOutput;
}
smallestCommons([1,5]);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0.
To point you in the right direction, take closer look at the lCMIterate function, you have two small bugs preventing reaching the base case of the recursive function.
Thank you / much appreciated. I couldn’t figure it out, so I actually completely restarted and solved it without using Euclid . Below is new code that just passed.:
function smallestCommons(arr) { //find min, max for the range of our function
var SCM = 0;
if (arr[0] > arr[1]) {
var max = arr[0];
var min = arr[1];
}
else {
var max = arr[1];
var min = arr[0];
}
//create an array of all numbers inclusive between min, max
//calculate maximum scm which is all numbers inclusive multiplied all together
var fullParameterArr = [];
var multipliedArray = [];
var multipliedAll = 1;
//generate by finding all combinations
for (var i = min; i <= max; i++) {
fullParameterArr.push(i);
multipliedAll*=i
multipliedArray.push(multipliedAll);
}
multipliedArray.sort((a,b) => b-a);
var maxSCM = multipliedArray[0];
var i = 1;
while(i*max <= maxSCM) {
var j = max-1;
var scmCountDown = fullParameterArr.length-1;
while ((i*max) % j === 0 && scmCountDown !== 0 && j >= min) {
scmCountDown--;
j--;
}
if (scmCountDown === 0) {
SCM = i*max;
break;
}
i++;
}
return SCM;
}
Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.
Thank you.
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.