Tell us what’s happening:
Hello, I am currently at the Intermediate Algorithm Challenges, the code that I wrote actually works fine and is showing the correct answer, I tested it on CodePen, but it still says that for the last two cases, it is still false. Is it because it is taking too long for it to compute? It takes around 5 seconds on Codepen, with [23, 18] as input
Thank you in advance!
EDIT: FCC console doesn’t even log anything when input is [23, 18], but it does output the result when the input are smaller numbers, for example [2, 10]
Your code so far
function smallestCommons(arr) {
arr.sort((a, b) => { return a - b });
let numToReduce = arr[0];
let sortedNumbers = [];
let arrOfTruth = [];
let checker = false;
let finalCheck = false;
let numToCheck = 1;
// Number range: arr[0] to arr[1]
for (let i = 0; numToReduce <= arr[1]; i++) {
sortedNumbers.push(numToReduce);
numToReduce++;
}
// As long as the final check is false, repeat
while (finalCheck == false) {
// Go through created number array
for (let i = 0; i < sortedNumbers.length; i++) {
// Check if number is divisable by arr nums, push booleans into array
if (numToCheck % sortedNumbers[i] == 0) {
checker = true;
arrOfTruth.push(checker);
} else {
arrOfTruth.push(checker);
}
checker = false;
}
// If only one element is false, the checked number is not correct
finalCheck = true;
for (let ele of arrOfTruth) {
if (ele == false) {
finalCheck = false;
}
}
// If all elements of truthArr are true, number is found
if (finalCheck == true) {
return numToCheck;
}
// Clear array for next number; increment the number to check
arrOfTruth = [];
numToCheck++;
}
}
console.log(smallestCommons([23, 18]));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0.
Your code is probably hitting the infinite loop protection and exiting early. If it takes too long to run your code, FCC will just stop executing it to protect your browser.
I’m not sure if the //noprotect still works in the redesigned app, but you can try that. You could also work on making your solution more efficient. All challenges can be solved in such a way that they complete before they time out. Or you can just skip the challenge. It’s your education, so it’s up to you.
It’s not working afaik, unless I used it in the wrong spot/s
function smallestCommons(arr) {
arr.sort((a, b) => { return a - b });
let numToReduce = arr[0];
let sortedNumbers = [];
let arrOfTruth = [];
let checker = false;
let finalCheck = false;
let numToCheck = 1;
// Number range: arr[0] to arr[1]
for (let i = 0; numToReduce <= arr[1]; i++) {
sortedNumbers.push(numToReduce);
numToReduce++;
}
// As long as the final check is false, repeat
//noprotect
while (finalCheck == false) {
// Go through created number array
//noprotect
for (let i = 0; i < sortedNumbers.length; i++) {
// Check if number is divisable by arr nums, push booleans into array
if (numToCheck % sortedNumbers[i] == 0) {
checker = true;
arrOfTruth.push(checker);
} else {
arrOfTruth.push(checker);
}
checker = false;
}
// If only one element is false, the checked number is not correct
finalCheck = true;
for (let ele of arrOfTruth) {
if (ele == false) {
finalCheck = false;
}
}
// If all elements of truthArr are true, number is found
if (finalCheck == true) {
return numToCheck;
}
// Clear array for next number; increment the number to check
arrOfTruth = [];
numToCheck++;
}
}
console.log(smallestCommons([23, 18]));