### re: Recursion recall, because it's necessary VIEW POST

Your explanation needs to be clarified, because I don't see what the recursive case is. Which function would be invoking itself?

Thank you for pointing that out. Going back to the find all even number example say I'll write a fucntion find all odd number *NOTE: code example used from Colt Steele's Javascript Algorithms and Data Structures Mastercass *

function collectOddValues(arr){

``````let result = []

function helper(helperInput){
if(helperInput.length === 0) {
return;
}

if(helperInput % 2 !== 0){
result.push(helperInput)
}

helper(helperInput.slice(1))
}

helper(arr)

return result;
``````

In this code the helper function makes ensures that there if there is no element given it exits out. The base case of the helper function is that it checks the zeroth item in the array set and checks to see if it's divisible by 2 with no remainder. If that condition is met, the element is cut from the array and the next element in the array is assessed until there are only odd elements left or there are no more elements

Next here's another code example for pure recursion *NOTE: code example from Colt Steele's Javascript Algorithms and Data Structures Masterclass *

function collectOddValues(arr){
let newArr = [];

``````if(arr.length === 0) {
return newArr;
}

if(arr % 2 !== 0){
newArr.push(arr);
}

newArr = newArr.concat(collectOddValues(arr.slice(1)));
return newArr;
``````

}

Here we're declaring a an empty array gets returned if there's nothing in the inputs. For our bas case we again check the zeorth element to see if results in an odd number and if that is the case, it gets pushed into a new array of odd values that gets sliced from the input once the collectOddValues function is called.

Hopefully I'm on the right track with this explanation and clarified what I missed. More feedback is appreciated and I hope this helps :)

What was missing was what you are recursing on, which is the tail of the input list. With the example code the recursion makes sense.

However, the "base case" isn't what you said -- the base case is the `if (are.length == 0) {`, which doesn't have anything to do with whether numbers are odd or even. This is what three me off.

Thanks for that I'll be more careful in the future

code of conduct - report abuse  