Smallest Common Multiple: Help

Tell us what’s happening:
My code is telling me that ‘min’ is not defined, I thought I had defined it, can someone please help me understand why it is not?
Thanks!

Your code so far


//noprotect
function smallestCommons(arr) {
  min = Math.min(null, arr);
  max = Math.max(null, arr);

  for(var i = 1; i <= 6056820; i++) {
    if(testCount() === true){
      return i;
    }
  }
}
function testCount(value) {
  for(var k = min; k <= max; k++){
    if(value % k !== 0){return false}
  }
  return true;
}



smallestCommons([1,5]);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36.

Link to the challenge:

You should use a spread operator if you want to pass an array into math.min and max, they will not work with arrays out of the box. Before a spread operator was around it required some voodoo magic with apply to get it to work :slight_smile:

The reason min is not defined is because you never defined it. min = something is not the same as const min = something. I make this same mistake almost every time I make a new variable :slight_smile: .

Thanks so much for your response! I tried utilizing const min = Math.min(...arr) and const max = Math.max(...arr) and I still can’t seem to get it to work :weary:

Omitting the let/const just made ‘min’ declared with var behind the scenes. Still a function scope, not using let/const when declaring variables is a terrible practice, but it was not the reason why his code didn’t work and certainly not the reason why min/max were undefined.
p.s. Nvm, scratch that, sorry, didn’t see that the test function is declared outside the main function. The point about min/max being undefined inside smallestCommons() because of the wrong usage of math functions still stands though.

Your problem is your algorithm.Think of ways you can find the smallest common multiple of a few numbers. Write the numbers out on a piece of paper, try to solve it yourself. Think about patterns. If you spend a good 30 minutes focusing on a problem and you are still stuck - take a break. Try again later, let your brain work on this problem in the background while you are doing something else.

If you are still stuck after a few pokes at this you can always look at hints - they do a good job of revealing to you pieces of the puzzle one by one. Don’t despair. You can do this!

First off, I just noticed when you call testCount() you didn’t pass in a value. You should fix that really quick.

As for min not being defined:
You have 2 functions defined in your code: smallestCommons(arr) and testCount(value).

min and max are defined in the first function, but not in the second. I believe the easiest ways to fix that would be
A.) get rid of the testCount function and just put that code in the if statement
or
B.) pass min and max to the testCount function a la testCount(value, min, max).
Then you should be able to use the variables min and max.