1

I wrote a simple program for learning purposes recently that (for now) basically just adds all the numbers together. But, for some reason, when I try to call it ( the programm is a function ), it puts out an "maximum call stack size exceeded" error. Is there any way to fix it? The answer may be very obvious, but I'm a very beginner at this stuff. Here's the code:

var add = function(num1, num2, num3, num4, num5) {
    for(i = 0; i < add.length; i++) {
if(i === 0) {
var first = add(i);
} else if(i === 1) {
var second = add(i);
} else if(i === 2) {
var third = add(i);
} else if(i === 3) {
var fourth = add(i);
} else if(i === 4) {
var fifth = add(i);
console.log(first + second + third + fourth + fifth);
};
    };
};

add(1, 2, 3, 4, 5);
Ry-
  • 218,210
  • 55
  • 464
  • 476
Niki45nk
  • 17
  • 4
  • Because you didn’t declare `i`, it’s a global variable and all your functions share the same one. Always use [strict mode](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode). But… maybe this wasn’t supposed to be recursive in the first place. Did you mean `arguments[i]`? There’s no reason to write that, though, because you can `console.log(num1 + num2 + num3 + num4 + num5)` without the loop. – Ry- May 06 '18 at 00:37
  • Well I was, and am currently, designing a quick programm that could add how many numbers you inputed in it, with the max being to be able to add up to 10 numbers. This code was just written for the startup to than later be improved upon to do the task. Oh, and I'm also a VERY begginer at this stuff and just know very few of the basics so sorry if I don't quite understand what you're saying. – Niki45nk May 06 '18 at 00:46
  • I’d recommend making that work on an array first – `add([1, 2, 3, 4, 5])` – before going into variable numbers of arguments. – Ry- May 06 '18 at 00:48
  • 2
    The function is currently invoking itself with `add(i)`, and it's attempting to do so an infinite number of times. If the recursion was intentional, then there should be a condition within the function which eventually allows it to stop and exit. – Otherwise, brackets rather than parenthesis can be used to access values in a collection (`coll[index]`). Also, a function's length tells you how many parameters were named in its definition rather than how many `arguments` it received when it was invoked. – Jonathan Lonowski May 06 '18 at 00:50

1 Answers1

1

I believe you are learning about arguments.

Since doing a add.length does not make any sense at all.

var add = function(num1, num2, num3, num4, num5) {
  for (i = 0; i < arguments.length; i++) {
    if (i === 0) {
      var first = arguments[i];
    } else if (i === 1) {
      var second = arguments[i];
    } else if (i === 2) {
      var third = arguments[i];
    } else if (i === 3) {
      var fourth = arguments[i];
    } else if (i === 4) {
      var fifth = arguments[i];
    }    
  }
  console.log(first + second + third + fourth + fifth);
};

add(1, 2, 3, 4, 5);

You can write it like this

var add = function() {
  var sum = 0;
  for (var i = 0; i < arguments.length; i++) {
    sum += arguments[i];    
  }
  return sum;
};

console.log(add(1, 2, 3, 4, 5));
console.log(add(1, 2, 3, 4));

Functions are supposed to take arguments, process and then return the result.

  • Well, I haven't yet learned about arguments...maybe that's the problem. Are they like the parameters in a function? – Niki45nk May 06 '18 at 00:56
  • @Niki45nk This should help explain: [What's the difference between an argument and a parameter?](https://stackoverflow.com/questions/156767/whats-the-difference-between-an-argument-and-a-parameter) JavaScript also offers [`arguments`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments) as an automatic variable within every `function`. – Jonathan Lonowski May 06 '18 at 01:01
  • You are confused about arguments and parameters. Parameter are like signature of you function when you define a function. But arguments are actual values passed to function while calling the function. – Vishnu Kyatannawar May 06 '18 at 01:05
  • Oh, get it now. Thank you kind sir! Would've taking me a while to figure that out with the website you send me! Thank you greatly :) P.S. I also noticed that instead of writing like: var first = arguments[i]; I wrote var first = arguments(i); Sorry about that... – Niki45nk May 06 '18 at 01:14