简体   繁体   中英

when i'm trying to view the entire array it gives me a single element

trying to create a recursive function that will take 2 parameters, starting number and a larger ending number and then it will count down / up (it really doesn't matter ).. i created an array to push - unshift the smaller number into and they i get the function to call it self again

function recursiveCountUp(startNum, endNum) {
let storageArray = [];

if(startNum>endNum)
{return [endNum];}
else{
         
storageArray.unshift(startNum);
recursiveCountUp((startNum+1),endNum);
        
}

return  console.log(storageArray);

}
  
recursiveCountUp(4,11);

i get the result as 
[ 11 ]
[ 10 ]
[ 9 ]
[ 8 ]
[ 7 ]
[ 6 ]
[ 5 ]
[ 4 ]

when i globally declared the array before the function i was able to ask for the array outside the loop and get the desired output from the function

let arrayRange = [];

function rangeOfNumbers(startNum, endNum) {

if(startNum>endNum)
{return [endNum];}
else{
arrayRange.push(startNum);
rangeOfNumbers((startNum+1),endNum);

}

}
`
rangeOfNumbers(4,11);

console.log(arrayRange);`

.. i understand that i'm getting the result a number by number as the function is executing the return every time it is being called

i need help in those two points:

  1. to get the full array i need to ask for the array outside the function, i can't do that if the array is declared locally ( in the mini assignment i'm doing it's asking me not to use globally declared variables )

  2. why am i getting the result like that: 11, 10, 9,8.. etc if i use push or i use unshift I'm getting the same result, and the output should start with the starting numb so i expected 4 first then 5 then 6.. etc

Point 1 - If you do not want to declare a global variable then you can pass the storageArray as an argument which will update every time you call a recursion function.

Point 2 - As you declare the storageArray variable every time you call the recursion function, it will store only a single element each time. So whether you use push or unshift, both operations are performed on a single element inside an array so you won't see much difference.

Here is a working snippet of passing an array as an argument.

 function recursiveCountUp(startNum, endNum, storageArray) { if (startNum > endNum) { return [endNum]; } else { storageArray.push(startNum); recursiveCountUp((startNum + 1), endNum, storageArray); } return storageArray; } let result = recursiveCountUp(4, 11, []); console.log(result);

This is how I'd write the code, if I had to use recursion:

 function recursiveCountUp(startNum, endNum) { return startNum>=endNum? [endNum]: [startNum, ...recursiveCountUp(startNum+1, endNum)] } console.log(recursiveCountUp(4,11))

Alternatively:

 function recursiveCountUp(startNum, endNum, arr=[]) { arr.push(startNum); if(startNum<endNum) recursiveCountUp(startNum+1, endNum, arr); return arr; } console.log(recursiveCountUp(4,11))

or, without recursion:

 function range(startNum, endNum) { return Array(Math.max(endNum-startNum+1,0)).fill().map((_,i)=>i+startNum) } console.log(range(4,11))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM