What I'm getting wrong with this recursion?

function recursion(arr, count){
if(count == 0){return} //{return arr => undefined}

arr = arr.map(n => n * 2)
recursion(arr, count - 1)
console.log(arr)
}
recursion(array, 5)
/*
[ 32, 64, 96, 128 ]
[ 16, 32, 48, 64 ]
[ 8, 16, 24, 32 ]
[ 4, 8, 12, 16 ]
[ 2, 4, 6, 8 ]
*/

I that the call stack looks at it should be, but if I try to exit the recursion (one count gets to 0) by returning the array it outputs undefined. Why so? (A simple loop works just fine…)

function test(arr){
  let count = arr.length
  while(count >= 0){
    arr = arr.map(n => n * 2)
    count--
  }
  return arr
}
test(array, 5)//[ 32, 64, 96, 128 ]

if you would post a link to the challenge
you are working on it would be helpful

Hi!

You works with arrays!
That means you have to return an empty array [] ==>
if(count == 0){ return []; }

It is not a challenge from the CV, not technically at least; also I’m not looking for a way to end the recursion, a simple return would be enough: I want to return the array that is passed to the function as an argument, on which we’ll perform a task of our choice a given number of times (the count argument), and finally return the array. It could be done with a loop, but I’d like to understand why is not working with recursion…

try looking at your function with this:
http://pythontutor.com/javascript.html

your function always return undefined - you never return anything else


function test(arr){
  let count = arr.length
  while(count >= 0){
    arr = arr.map(n => n * 2)
    count--
  }
  return arr
}
console.log(test([ 32, 64, 96, 128 ], 5));

logs …

  1. 0: 1024
  2. 1: 2048
  3. 2: 3072
  4. 3: 4096
function moreRecurse(arr, n){
 if(n == 0) return []
 let finalArray = moreRecurse(arr.map(el => el * 2), n - 1)
 
 finalArray.push(arr)
 return finalArray
}

  moreRecurse([1, 2, 3], 4)//[ [ 8, 16, 24 ], [ 4, 8, 12 ], [ 2, 4, 6 ], [ 1, 2, 3 ] ]

I’m getting it a little better, thanks for you replies guys. As for the case above, my question is: how do each function -while getting back to the original one- have access to arr? Each time, as the callStack deflates, I push arr to the finalArray, and then I return it (finalArray I mean), but I’m not returning arr itself. Is that a default behavior of the recursive function to ‘return’ its values? Arr is mapped and then -under the hood- gets available to the functions that follows? To make myself clearer:

let count = 5
let array = [1, 2, 3]
let finalArray = []
while(count > 0){
  finalArray.push(array.map(el => el * 2))  //arr is always the same of course, therefore is always the same array that gets pushed to the final one
  count--
}
console.log(finalArray)//[ [ 2, 4, 6 ], [ 2, 4, 6 ], [ 2, 4, 6 ], [ 2, 4, 6 ], [ 2, 4, 6 ] ]

A solution could be:

let counter = 5
let another_array = [1, 2, 3]
let another_finalArray = []
while(counter > 0){
  another_array = another_array.map(el => el * 2)
  another_finalArray.push(another_array)
  counter--
}
console.log(another_finalArray)
/*
[ [ 2, 4, 6 ],
  [ 4, 8, 12 ],
  [ 8, 16, 24 ],
  [ 16, 32, 48 ],
  [ 32, 64, 96 ] ]
 */

That’s the point where I’m getting confused; I cannot see the process, in my recursion, of ‘redefining’ the array before pushing it into the finalArray.

Break. Before posting I took my time and I tried to figure it out. What happens is something like that right?

  /*
//the number is the nth line

 (((1)2)3).map() 4th line => our final array is ready
 
//before getting to the final line, at each line arr is mapped and -as a wave- gets finally to the 4th line, where our function stops running and returns our final result... 
   */

here you are passing the mapped arr to the next function call - note you are not changing arr here for this function

you can probably see it a little better if we would explicit everything as much as possible:

function moreRecurse(arr, n){
 // if n is 0, return an empty array
 if(n == 0) {
   return []
 }
 // create new array with all elements doubled
  let mappedArray = arr.map(el => el * 2)
 // call the function to get an array with higher numbers
  let finalArray= moreRecurse(mappedArray, n-1);
 // add current array at the end of the higher numbers array
 finalArray.push(arr)
 // return array with all numbers
 return finalArray
}

try looking at this with the pythontutor tool: Online JavaScript Compiler, Visual Debugger, and AI Tutor - Learn JavaScript programming by visualizing code

Thank you, I finally figure it out.