0

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));
5
  • have you tried debugging it with a debugger? Commented Dec 2, 2014 at 13:35
  • on return i have put console.log which show its properly, but outside function its undefined Commented Dec 2, 2014 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. Commented Dec 2, 2014 at 13:39
  • 1
    usually SO downvote question if it not correspond SO rules for question like How to Ask Commented Dec 3, 2014 at 6:18
  • This is obviously a very old question now but out of curiosity did you simply want to populate an array with consecutive numbers? I found this answer useful for this problem specifically stackoverflow.com/questions/3895478/… Commented May 10, 2019 at 13:24

3 Answers 3

3

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

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

5 Comments

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 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.
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 ?
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 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.
0

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);

Comments

Your Answer

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.