-2

I've got the following function:

function getRange() {

    // generate a range of values from value up to climit
    // excluding climit
    var countup = function(value, climit) {
        if (value < climit) {
            console.log(value);
            return countup(value + 1, climit);
        }
    };

   // gather input from the form
   var x = document.getElementById("range_form").range_x.value;
   var y = document.getElementById("range_form").range_y.value;

   // Validate the input
    if (isNaN(x) || isNaN(y)) {
        console.log("Both values have to be integers!!!");
    } else {
        // run the countup fonction
        countup(x, y);
    }
}

Now function countup (on its own) works as expected by me. Once I've put it within the getRange function, it stopped working properly. When I put some values in the form (eg. 2 and 9) the output in the console is endless 1111111111111111 and then the error: Uncaught RangeError: Maximum call stack size exceeded.

Please advise

Wasteland
  • 4,889
  • 14
  • 45
  • 91
  • 1
    since you are seeing `1111111111111111` it would appear that you are working with a string instead of an integer. Try wrapping your var x, y variables in `parseInt` (e.g. `parseInt(document.getElementById("range_form").range_x.value, 10)` – Rob M. Oct 26 '15 at 19:37

1 Answers1

2

x and y are strings. Therefore,

  • value + 1 causes concatenation, and
  • < does a lexical comparison (e.g., "211111" < "3" is true)

Convert your input to numbers using parseInt(..., 10):

var x = parseInt(document.getElementById("range_form").range_x.value, 10);
apsillers
  • 112,806
  • 17
  • 235
  • 239
  • Thank you both. That was it. What's the meaning of ,10 at the end? – Wasteland Oct 26 '15 at 19:40
  • 1
    [That's the radix](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt): "An integer between 2 and 36 that represents the radix (the base in mathematical numeral systems) of the above mentioned string. Specify 10 for the decimal numeral system commonly used by humans. Always specify this parameter to eliminate reader confusion and to guarantee predictable behavior." – Andy Oct 26 '15 at 19:42
  • 1
    @Wasteland It enforces base 10. Without it, hexidecimal is also allowed (e.g., `parseInt("0xdeadbeef")` bases the whole hex value, while `parseInt("0xdeadbeef", 10)` only parses the first zero and stops at the `x`) – apsillers Oct 26 '15 at 19:42