Where do I put "return"?

Where do I put "return"?
0.0 0


Here’s my code. (I’m sure there’s a better way to do this, but I want to muscle my way through on my own before looking at other solutions.)

function smallestCommons(arr) {
  var lesser, greater;
  if (arr[0] < arr[1]) {
    lesser = arr[0];
    greater = arr[1];
  } else {
    lesser = arr[1];
    greater = arr[0];

  var factorsArray = [];
  var multiplesOfGreater = [];

  for (var i = lesser; i <= greater; i++) {
    factorsArray.push(i); //put in the integers between the two values
  var limit = factorsArray.reduce(function(a, b) {
    return a * b;
  }); //the SCM is never going to be more than the product of all the numbers.
  for (var j = 1; j <= limit / greater; j++) { //we're going to start making multiples of greater, starting with itself x 1. We will go until we hit the limit (i.e. until j is a number that, multiplied with greater, equals limit).
    var currentMultiple = greater * j;
    for (var k = 0; k < factorsArray.length; k++) { //now we'll ask whether this multiple is divisible by each of the integers in factorsArray in turn.
      if (currentMultiple % factorsArray[k] !== 0) {
        break; //as soon as it's not divisible by one of the integers, our work here is done and let's consider a different multiple. Break this for loop (k) and do the next iteration of the outer one (j)
      } else {
        //if it is divisible, go on to check the next integer. Repeat this for loop with a different k.
      return currentMultiple; //if it gets this far, it's passed every integer; this is the answer

My premise, with those nested for loops, is “if we can get through all of factorsArray, proving that the multiple of greater in question is divisible by each, then it’s our answer.” But if I put the return line inside the second for loop, it never gets to it because it ends the final iteration with continue; and peters out. If I put it inside the first, outer for loop, it ends the whole thing after the very first break; and returns nothing. How can I make this work?


@AbdiViklas you could var currentMultiple outside of your j loop and just store your results in variables.
Then return currentMultiple after the j loop


That does’t answer the question.

You use Return when you want your code to exit your function.

function (){