Not sure why if statement isn't activating

Tell us what’s happening:

Your code so far

function smallestCommons(arr) {
let smallNum = 0
let bigNum = 0
// Finds which number is larger in the array
if (arr[0] > arr[1]){
  bigNum = arr[0]
  smallNum = arr[1]
  bigNum = arr[1]
  smallNum = arr[0]
//initializing counter + return value
let counter = 0
let finalNum = 0
//Goes to an arbitrary large number as the value to compare to provided values
for (let i=bigNum; i < 999999999; i++){
  counter = 0
  // Test each number on each provided value
  for (let j=smallNum; j <= bigNum; j++){
    //if it fits, counter + 1
    if (j%i === 0){
      // if counter = number of values between provided values, assigns return value 
      // to the number which is being compared
      if (counter === bigNum - smallNum-1){
        finalNum = i
return finalNum;


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36.

Challenge: Smallest Common Multiple

Link to the challenge:

I tried to accomplish this problem using a nested for loop and a bunch of if statements, and while the execution gets almost all the way up the tree (tested this using console.log) it never activates the final if statement. I tried seeing if maybe I was supposed to do -1 or +1 for the value the counter needs to be at, however I could only get it to work with -3 with values of 1 and 5, meaning the counter isn’t going above 1. Could someone identify why the counter seems to stop working after it works the first time causing the if statement to never activate?

//Goes to an arbitrary large number as the value to compare to provided values
for (let i=bigNum; i < 999999999; i++){

This is a very bad idea.

1 Like

I know it would take up extra processing power and complicate things but i figured it would work for the problem since I had a break at the end. Would it be better to simply scrap the nested for loops and start from scratch using another method?

Ah ha! Your bug is here:

    if (j%i === 0){

The remainder when you divide j by i? This isn’t doing what you want.

You can clean up this a lot by incrementing i smarter and improving your j check accordingly. I’d also return i instead of trying to break out of the loops. It’s easier.

But to get good performance, I’d look up on Wikipedia:

  1. Computing the LCM of a range of numbers
  2. Computing the LCM from the GCD of two numbers
  3. Computing the GCD efficiently with a small recursive formula
1 Like

!! thank you so much dude. Immediately understand at least the bug I had even though it still wont work with just that fix. Thanks a ton for recommending specific things to read up on, that’s extremely helpful.

Edit: Works perfectly when I return i instead. :DDD - also did +1 instead of -1 as the code on the forums show

With just that fix, I think you only need to fix the range in your innermost if statement and return instead of break and it works for me?

1 Like

Yessir, you’re an absolute beast i really appreciate it

1 Like

I’m happy to have helped.