0

I have the following code, trying to return the range between two numbers, and the inner function is not returning a value. It simply returns [Function]. Can anyone tell me why?

var range = function(num1, num2) {
    var output = [];  
    return function range2 () {
        if (num2 - num1 === 2) {
            return [num1+1];
        }
        else if (output.length + 1 === num2 - num1) {
            return output;
        }
        else if (output.length + 1 !== num2 - num1) {
            output.push(num1 + 1);
        }
        return range2(num1 + 1, num2);
    };
};

//UPDATE: This is my code that ended up working, FWIW:

var range = function(num1, num2) {
    var list = [];
    var range2 = function(num1, num2) {
        list.push(num1 + 1);
        if (num2 - num1 === 2) {
            return list;
        }
        else {
            return range2(num1 + 1, num2);
        }
    };
    return range2(num1, num2);
};
3
  • 1
    return function ... returns the function, it doesn't call it. Commented Jan 27, 2016 at 21:45
  • you need to execute the function,, so have: return function range2(){}(); Commented Jan 27, 2016 at 21:45
  • You need parentheses around the definition to call it inline, @Fallenreaper. return (function range2(){...})();. Commented Jan 27, 2016 at 21:53

2 Answers 2

1

Your return will return the definition. If you are trying to return the execution of the function you will need to add a ()

See here:

var range = function(num1, num2) {
  var output = [];  
  return function range2 () {
    if (num2 - num1 === 2) {
        return [num1+1];
    }
    else if (output.length + 1 === num2 - num1) {
        return output;
    }
    else if (output.length + 1 !== num2 - num1) {
        output.push(num1 + 1);
    }
    return range2(num1 + 1, num2);
  }();  // <----  this will execute the defined function.
};
Sign up to request clarification or add additional context in comments.

3 Comments

This returns an array of the correct length, but each value === num1+1. So the arguments aren't getting updated somehow?
@user5369036 range2() doesn't take any arguments. So when you call range2(num1 + 1, num2), those parameters have no effect.
@Barmar Ahh, gotcha.
0

The range() function never calls range2(). It simply returns the function expression.

Also, range2() needs to take arguments.

var range = function(num1, num2) {
  var output = [];

  function range2(num1, num2) {
    if (num2 - num1 === 2) {
      return [num1 + 1];
    } else if (output.length + 1 === num2 - num1) {
      return output;
    } else if (output.length + 1 !== num2 - num1) {
      output.push(num1 + 1);
    }
    return range2(num1 + 1, num2);
  };
  return range2(num1, num2);
};

alert(range(1, 10));

4 Comments

So this returns an array of a single value, num2-1. Probably has to do with the closure?
It only does that when num2 - num1 === 2. That's the first line of range2.
range(1, 10) returns [2, 3, 4, 5]
I don't really understand the logic of your algorithm, I ignored that. I just showed how to fix the syntax so that the recursive function is called.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.