0

if I have a simple test function that adds even numbers to an array:

function isEven(n){
 var enumbers = [];
 if (n % 2 == 0){
 enumbers.push (n);
 }
}

how can I increment my parameter until I have a set number of members in my array? for instance, I've tried this:

function isEven(n){
 var enumbers = [];
 while ( enumbers.length < 10){
 if (n % 2 == 0){
 enumbers.push (n);
 }
 console.log (enumbers);
 n = n + 1;
 isEven(n);
 }
}
isEven(1);

but it seems to just create a new array for each number until it finally throws a range error (maximum call stack size exceeded).

asked Jun 20, 2013 at 19:54
11
  • 3
    Test against enumbers.length? Commented Jun 20, 2013 at 19:56
  • your calling isEven recursivly inside your loop. So for every function call you actually call your function 10 additional times. Commented Jun 20, 2013 at 19:58
  • that was a typo... i had already corrected that but I got the same result. Commented Jun 20, 2013 at 19:59
  • 1
    What is this function supposed to do? Commented Jun 20, 2013 at 20:02
  • keep evaluating numbers 1 through however many it takes to push a predetermined (in this case, 10) number of even numbers into the "enumbers" array. Commented Jun 20, 2013 at 20:06

5 Answers 5

2

It's creating that array multiple times because you're constantly calling that function with:

isEven(n);

You're also not comparing to the length of the array, just the array. Add .length to enumbers. Try changing to:

var enumbers = [];
while ( enumbers.length < 10){
 if (n % 2 == 0){
 enumbers.push (n);
 }
 console.log (enumbers);
}
answered Jun 20, 2013 at 19:58

4 Comments

The strategy was to keep calling isEven until the array had ten members. That's why I put it in a while loop. I think I just need to know how to properly increment the function parameter isEven(?)
You don't need to keep calling that, because each time you do, it re-initializes the array. Your while loop is taking care of the atleast 10 numbers part.
@kjarsenal: Consider either calling a function that puts one number in the array repeatedly (recursively), or putting that functions' body in a loop. Not both.
Thank you Bergi and Vitim. I think I misunderstood the way the while loop operates. Thanks for your help.
0

I'm not sure if I understood your question. But you shouldn't use global variables, and it is unnecessary to call your function recursively inside a while loop.

The error maximum call stack size exceeded is your browser trying to break a infinity loop.

This is what you need.

Examples here jsFiddle1 and jsFiddle2

function isEven(n) {
 var enumbers = [];
 while (enumbers.length < 10) {
 if (n % 2 == 0) {
 enumbers.push(n);
 }
 n++;
 }
 return enumbers;
}

Setup a test

var n = 1;
var evenArray = isEven(n); //call isEven function and it returns an array
document.body.innerHTML = evenArray; //2,4,6,8,10,12,14,16,18,20
answered Jun 20, 2013 at 20:15

Comments

0

The problem is that (enumber < 10) apparently always evaluates to true, causing an endless loop. But it is this comparison that is wrong, since you're comparing an integer with an array I think you're trying to get the array length?:

while (enumbers.length < 10) {

Another thing. enumbers is a local variable, so each call to isEven has it's own array. Therefore, the functions is called recursively, over and over again.

answered Jun 20, 2013 at 19:57

1 Comment

That is now fixed. However, try [0,2,4] < 10...
0

I suggest you create the array outside of is even method

answered Jun 20, 2013 at 20:00

Comments

-1

I would have written something like:

function isEven(n,enumbers){ 
 while(enumbers < 10){ 
 if (n % 2 == 0){ 
 enumbers.push (n); 
 }
 console.log (enumbers); 
 n = n + 1; 
 isEven(n, enumbers); 
 } 
} 
var enumbers = []; 
isEven(1,enumbers);
Victor
22.4k15 gold badges101 silver badges115 bronze badges
answered Jun 20, 2013 at 20:00

2 Comments

this actually is exactly what I was trying to accomplish. You just copied my codebase before I remembered to add .length to enumbers. this is great
Please either use recursion or a while-loop

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.