Tell us what’s happening:
Hi! This is my first time posting to the forums, so please forgive me for any transgressions; particularly the sloppy code/commenting.
I had two questions regarding this challenge:
A: When I comment out the console.log’s I’m using to debug at 19, 23, and 27, for some reason, some of the fulfilled stories are no longer fulfilled. For instance, if the console.log at 27 is commented out, I fulfill the first 4 requirements of arguments passed through my function, but when that line isn’t commented out, I only pass the very first set of arguments. Shouldn’t the console.log commands not affect the rest of the code?
B: My code is able to fulfill the first 4 sets of arguments, but I can’t figure out why the last two sets aren’t going through. When I attempt to debug via console.log to see what’s going on; it seems my ‘j’ in the main for loop isn’t going all the way to its limiter that I listed. Any ideas?
Again, this is my first forum post, so I apologize if I’ve done anything redundant or out of turn. Thank you in advance, this site has been absolutely stellar so far for a brand new coder!
Your code so far
function smallestCommons(arr) {
let args = arr.sort((a,b) => a - b);
let argSpread = [], argsArrangeSpread = [];
let argMin = args[0] * args[1];
let finalNum = 0;
var numCount;
for (let i = args[0]; i <= args[1]; i++) {
argSpread.push(i);
}
var argMax = argSpread.reduce((a,b) => a*b);
argsArrangeSpread = argSpread.sort((a,b) => b-a);
let spreadLength = argsArrangeSpread.length;
for (let j = 1; j < argMax; j++) {
numCount = 1;
if (finalNum == 0) {
//console.log(numCount, j, argMax, spreadLength);
for (let k = 0; k <= spreadLength; k++) {
if (j % argsArrangeSpread[k] == 0) {
//console.log(numCount, j, spreadLength, arr, argMax, "this one works");
numCount++;
continue;
} else if (numCount == spreadLength + 1) {
// console.log("this time", j);
finalNum = j;
break;
} else {
//console.log(numCount, j, argsArrangeSpread.length, "next", arr, argMax)
break;
}
}
} else {
break;
}
}
//console.log(finalNum, arr);
return finalNum;
}
smallestCommons([1,5]);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/smallest-common-multiple