# Smallest common multiple - works in browser, won't pass last freeCodeCamp test [SOLVED]

Smallest common multiple - works in browser, won't pass last freeCodeCamp test [SOLVED]
0

#1

This code works in my browser for all of the freeCodeCamp tests, but won’t pass the last test at freeCodeCamp:

`smallestCommons([23,18]);`

``````function smallestCommons(arr) {
//find smallest common multiple between the range of two numbers

var newarr = []; // lower and upper limit in order
var newarr2 = []; // range between lower and upper limit
var firstValue = 0; // firstValue of newarr2[];
var nextValue = 0; // firstValue + 1
var lastValue = 0; // lastValue of newarr2[]
var leastCommon = 1;
var numToLoop = 0; // lastValue of newarr2[]*number of loops
var leastCommonArr = []; //array to hold true, false
var leastCommonArrTrue = []; //array of all true for compare
var foundIt = false;  // trigger to stop trying
var upperLimit = 1;  // all values in newarr2[], multiplied together

//1 put the numbers in order
if (arr[0] < arr[1]) {
newarr.push(arr[0]);
newarr.push(arr[1]);
} else {
newarr.push(arr[1]);
newarr.push(arr[0]);
}
console.log("sorted newarr[] is: " + newarr);

//1.5 start the newarr2 array
//newarr2.push(newarr[0]);
//2 add array of numbers from newarr[0] to newarr[1]
firstValue = newarr[0];
console.log("firstValue is: " +firstValue);
lastValue = newarr[1];
console.log("lastValue is: " +lastValue);
nextValue = firstValue;
console.log("nextValue is: " +nextValue);

for (var i = 0; nextValue <= lastValue; i++ ) {
newarr2.push(nextValue);
// console.log(newarr2);
nextValue = nextValue + 1;
// console.log("nextValue is: " +nextValue);
}
console.log("newarr2 with incremental #s is: " + newarr2);

//2.5 create array of false values for leastCommonArr
for (var p = 0; p < newarr2.length; p++ ) {
leastCommonArr.push(false);
}
//  console.log("leastCommonArr  is: " + leastCommonArr);

//2.6 create array of true values for leastCommonArrTrue
for (var q = 0; q < newarr2.length; q++ ) {
leastCommonArrTrue.push(true);
}
//  console.log("leastCommonArrTrue  is: " + leastCommonArrTrue);

//2.7 find upperlimit by multiplying all values in the array
for (var s = 0; s < newarr2.length; s++){
upperLimit = upperLimit * newarr2[s];
console.log("upperLimit is: "+ upperLimit);
}

//3  find least common multiple by working through the multiples of the last number
lastValue = newarr2[newarr2.length - 1];
//4 test to see if leastCommon is divisible by the other values in the array
//leastCommon starts as lastValue
leastCommon = lastValue;
numToLoop = newarr2.length;
console.log("numToLoop: " + numToLoop);

var n = 2;
while( n <= upperLimit) {
for (var m = 0; m < numToLoop; m++) {
if(leastCommon % newarr2[m] === 0) {
leastCommonArr[m] = true;
}
//	console.log("n is: " + n + " and m is: " + m);
//	console.log("leastCommon is : " + leastCommon);
//	console.log("newarr2[m] is: " + newarr2[m]);
//	console.log("leastCommon % newarr2[m] is :" +  leastCommon % newarr2[m]);
//	console.log("leastCommonArr is : " + leastCommonArr);
//	console.log("---------");
}

for (var x = 0; x < leastCommonArr.length; x++ ){
if (leastCommonArr[x] === leastCommonArrTrue[x]) {
foundIt = true;
}
else {
foundIt = false;
break;
}
}

if (foundIt === true) {
break;
}

//reset leastCommonArr and increment n
for (var y = 0; y < newarr2.length; y++ ) {
leastCommonArr[y] = false;
}
//  console.log("leastCommonArr reset is: " + leastCommonArr);
leastCommon = lastValue * n;
n = n + 1;
}

console.log(leastCommon);
// leastCommon = leastCommon - lastValue;
// console.log(leastCommon);
return leastCommon;
}
``````

#2

You need to use triple backticks to post code to the forum.
See this post for details.

#3

Thank you. First posting.

#4

I found out, on the github posts, that if I add // noprotect to the top of this code freecodecamp will accept it. Here’s freecodecamp’s discussion of // noprotect:

I think this is freecodecamp’s way of telling me that although my code works, it could definitely be refactored to be more efficient. Bottom line: if your code works locally but doesn’t pass all of the involved tests a freecodecamp you might try this but don’t use // noprotect outside of freecodecamp.