Problem: I've been having
to write solutions which required some sort of recursion. The last recursion
assignment I had to write was a homework assignment about 10 years ago. I
remembered the concept, but forgot how to write it.
Solution: Here’s a simple example of recursion. I start
with an array, and after I fire the function, I remove that index from the
array, so it shrinks. Once the array is empty, recursion stops. Pretty straight forward!
Code:
let books = ['Chosen by God', 'Holiness of God', 'Essential Truths', 'Justified by Faith'];
function readBooks(b){
while(b.length > 0){
console.log('I\'v read ' + b[0]);
b.shift();
readBooks(b);
}
}
readBooks(books);
Comments
Post a Comment