Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I am trying to fill an array with ranges of values in a recursive function, but i see a weird thing happening while returning the array, though the array has values, when i alert it outside the function it gives me undefined. Not Sure whether its my code issue or any kind of behavior.

Same implementation when i tired using simple for loop it worked fine.

I don't know what title to give for this problem, please suggest me a good one.

JS Fiddle

With Recursion

var arr = [];
function fillArray(n,i){
    if(arr.length !== n){
        if(i===0)
            arr[i] = i;
        else
            arr[i] = arr[i-1] + 1;
        fillArray(n,++i);
    }
    else{
         console.log(arr);
         return arr;
    }
}
console.log(fillArray(10,0));

With For Loop

var arr = [];

function fillArray(start,end){
    for(var i=start,j=0;i<=end;i++,j++){
        arr[j] = i;
    }
    return arr;
}

alert(fillArray(1,10));
share|improve this question
    
have you tried debugging it with a debugger? – Daniel A. White Dec 2 '14 at 13:35
    
on return i have put console.log which show its properly, but outside function its undefined – Duster Dec 2 '14 at 13:36
    
Suggestion to Stack Overflow, if anyone or stack overflow is down voting the question, please give a reason. it would help when people raise question next time. – Duster Dec 2 '14 at 13:39
1  
usually SO downvote question if it not correspond SO rules for question like How to Ask – Grundy Dec 3 '14 at 6:18

First, this is not a good example of something that should be implemented recursively in JavaScript. It's unidiomatic.

The reason the result is undefined outside the function is that your code fails to return the result of each successive recursive call. The recursion statement should look like:

    return fillArray(n,++i);

Without that, the final call that does return the array will have its return value simply ignored by the penultimate call.

share|improve this answer
    
i am using recursion here is avoid loop... is there any way to insert to array same set of values like 100 1's without loop ? – Duster Dec 3 '14 at 11:55
    
@Duster why do you want to avoid a loop? JavaScript does not have proper tail calls, so recursive functions like yours use memory for no good reason. There's nothing wrong with a loop. – Pointy Dec 3 '14 at 15:22
    
i actually wanted to achieve display of prime number between a range in a single loop. so that my complexity would not be n*n. Is there any way to do that then ? – Duster Dec 4 '14 at 7:32
    
I think i can fill the array with out using loop like how you told in one of the conversation here : stackoverflow.com/questions/10073699/… – Duster Dec 4 '14 at 7:47
    
@Duster using recursion instead of iteration does not necessarily have any effect on the time complexity of a solution. Recursion is just another way of iterating. – Pointy Dec 4 '14 at 13:37

Take a look on your example:

var arr = [];
function fillArray(n,i){
    if(arr.length !== n){
        if(i===0)
            arr[i] = i;
        else
            arr[i] = arr[i-1] + 1;
        fillArray(n,++i); // ends without returning anything
    }
    else{
         console.log(arr);
         return arr; // return array
    }
}
console.log(fillArray(10,0));

First of all I wouldn't declare value outside function and I wouldn't do with recursion (as you pollute your closure. But if you do so and you keep your function as it is, don't expect value as return here (as you edit variable outside of it).

var arr = [];
function fillArray(n,i){
    if(arr.length !== n){
        if(i===0)
            arr[i] = i;
        else
            arr[i] = arr[i-1] + 1;
        fillArray(n,++i);
    } // return not needed
}
fillArray(10,0);
console.log(arr);
share|improve this answer

function fillArray(n, i, a) {
  a.push(i);
  return a.length < n ? fillArray(n, ++i, a) : a;
}

console.log(fillArray(10, 0, []));

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.