-1

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));
Duster
  • 31
  • 2
  • 7
  • have you tried debugging it with a debugger? – Daniel A. White Dec 02 '14 at 13:35
  • on return i have put console.log which show its properly, but outside function its undefined – Duster Dec 02 '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 02 '14 at 13:39
  • 1
    usually SO downvote question if it not correspond SO rules for question like [How to Ask](http://stackoverflow.com/help/how-to-ask) – Grundy Dec 03 '14 at 06: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 https://stackoverflow.com/questions/3895478/does-javascript-have-a-method-like-range-to-generate-a-range-within-the-supp – Scott Anderson May 10 '19 at 13:24

3 Answers3

2

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

console.log(fillArray(10, 0, []));
cstuncsik
  • 2,241
  • 2
  • 14
  • 17
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.

Pointy
  • 371,531
  • 55
  • 528
  • 584
  • 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 03 '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 03 '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 04 '14 at 07:32
  • I think i can fill the array with out using loop like how you told in one of the conversation here : http://stackoverflow.com/questions/10073699/pad-a-number-with-leading-zeros-in-javascript/10073788#10073788 – Duster Dec 04 '14 at 07: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 04 '14 at 13:37
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);