Monday 15 April 2013

javascript - proper use of the array.push method -


If I have a simple test function that adds a number to an array:

  Function also (n) {var nbbs = []; If (n% 2 == 0) {enumbers.push (n); }}   

How do I extend my parameters until I have a fixed number of members in my array? For example, I have tried to:

  function isEven (n) {var enumbers = []; While (nabell.line and lt; 10) {if (n% 2 == 0) {enumbers.push (n); } Console.log (enumbers); N = n + 1; IsEven (n); }} Is also (1);   

But it seems that only a new array is created for each number, as long as it eventually throws a threshold error (the maximum call stack size is exceeded).

I'm not sure if I understood your question but you should not use global variables, and It is unnecessary to call your function again within the loop.

Error Max call stack exceeds size Your browser is trying to break an infinity loop.

This is what you desired.

Example here and

  isEven (n) {var enumbers = []; While (nabell.line and lt; 10) {if (n% 2 == 0) {enumbers.push (n); } N ++; } Return number; }   

Set up a test

  var n = 1; Var is also Array = (N); // Call is also a function and it gives an array document. Nobody.innerHTML = evenArray; // 2,4,6,8,10,12,14,16,18,20    

No comments:

Post a Comment