Smallest Common Multiple - Last two tests not working

Smallest Common Multiple - Last two tests not working
0

#1

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.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/smallest-common-multiple/


#2

#3

Unfortunately, I am not getting any infinite loop protection message.


#4

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.


#5

Is there anything else I can do besides just copying the given solution and be done with the challenge?


#6

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.


#7

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]));

#8

I believe that you needed to put //noprotect at the top of the code. But as I said, that command may not currently exist.


#9

Use this relationship to make the code more efficient

LCM of two numbers (a, b) is just (a*b)/GCD(a,b)
GCD of consecutive numbers is always 1.


#10

What does LCM and GCD stand for?


#11

LCM is lowest common multiple. GCD is greatest common divisor.


#12

Okay, thank you, I might figure out and/or implement that into my function. :sweat_smile: