Recursion: A free course with challenges + video

Recursion is one of the most complex and challenging concepts in all of programming! This free course outline of the CSX Unit on Recursion will begin by digging into the fundamentals. CSX is Codesmith’s free online JavaScript learning journey, you can make an account and work through the Recursion Unit here.

This CSX unit develops the intuitions necessary to tackle recursion problems effectively. It cover structural and generative recursion and visualize recursive code execution to help prepare you for many of the most popular recursion interview questions.

This unit is made up of 6 videos, 5 challenges, and 1 in person (LA + NY) /online interactive workshop to give you a full introduction to the concept of recursion and help you test out your understanding.

Now let’s dive in…

An introduction to Understanding Recursion

The unit starts with an introductory video to recursion by Codesmith Lead Technical Mentor, George Norberg (now Software Engineer at Cornerstone OnDemand).

He starts you off with a question…

Now we know in JavaScript functions can call other functions. What happens in JavaScript when a function tries to call itself?

Think about this one for a bit and begin mastering recursion with George’s video on how to effectively understand recursion & get kicked off with your first challenge to test that understanding.

You’ll soon be able to laugh politely at this joke ;) —

People often joke that in order to understand recursion, you must first understand recursion.
- John D. Cook

Here is George’s code from the explanation if you’d like to try it yourself:

let counter = 0;
function recurse() {
if (counter ===2) return 'done';
counter++;
return recurse()
}
const output = recurse();
consol.log(output)

Challenge: Repeater

After grasping what recursion is and before starting any challenges I recommend searching ‘recursion’ on google.

You’ll see that google then asks you if you meant recursion. Click their recommendation and see what happens.

If this doesn’t make you crack a smile I suggest watching the introduction video again until it does!

After learning the fundamentals of recursion, your ready to test that with the repeater challenge. A fundamental way of learning with CSX is to learn through doing. It’s easy to listen to a video and think “aha, I get it now!” but to truly understand a concept you must really work with that concept through algorithmic problems or if possible — building a project requiring that concept.

Repeater is the first challenge in the CSX Recursion unit and you’ll write a function that takes an input and returns that input a number of times using recursion.

Input: {String} char
Output: {String}

The CSX platform allows you to check your answers, so you’ll be able to test your solutions to this challenge to see if you’re successfully using recursion.

Solution: Repeater

After completing the repeater challenge, George will guide you through his solution for the challenge as well as introduce you to his style of problem solving.

Make sure not to watch this video until you’ve solved the challenge, pushing through blocks and struggling through a challenge will benefit you much more then immediately watching a solution for it. This is a very important lesson in coding (and life!) and a way of teaching and learning done at Codesmith in every facet.

As George says in the solution video —

Learning is in the struggle!

As George mentions, anything that can be done with a for loop or a while loop can be done with recursion — so make sure that you solved this challenge using recursion.

Now let’s watch George explain —

Parameters as Storage

George will now explain how to take a more functional programming mindset (no side effects or global variables) to function calls in recursion!

This will help you clean up your recurse functions so that everything you want to do is included in the function call itself so that every time you call a function with some input you can guarantee that you get the same output.

Here is George’s code from the explanation if you’d like to try it yourself:

function recurse(counter = 0) {
if (counter ===2) return 'done';
const newCounter = counter + 1;
return recurse(newCounter);
}
const output = recurse();
consol.log(output);

Challenge: Factorial

Now that you have a deeper understanding of not only recurse functions but how to write clean recurse functions it’s time to move on to the next challenge — the Factorial challenge.

Try to do the factorial challenge without influencing anything globally (re watch the last video of George explaining paramaters as storage if you’re confused by this).

In this challenge you’ll write a function that returns the factorial of a number using recursion.

For example : 4! = 4 * 3 * 2 * 1 = 24, so calling factorial(4) should return 24.

Input: {Number} num - number whose factorial is sought
Output: {Number}

If you have questions on any of these challenges, be sure to post them in the CSX Slack Channel so other CSX users and Codesmith mentors can guide you toward understanding the problem and help you find a solution. You can join the CSX Slack Channel here.

Solution: Factorial

After completing the factorial challenge, you’ll next watch George explain the solution for it.

Factorial is a classic recursive algorithms, so if you were able to do this that’s fantastic! You now have the basics of recursion down, and you’re ready to move on to more complex and abstract algorithms.

Throwing this in here as a reminder to have a bit of fun with recursion :)

Performance in JavaScript and Challenge: getLength

Other functional programming languages like pascal and scala are completely optimized to make function calls. That means things like recursion are simple for their engines to perform.

JavaScript additionally has the object oriented programming paradigm, therefore the JavaScript engine isn’t optimized to handle recursion like other functional programming languages are.

This leads to simple recursion in JavaScript creating a looping behavior before hitting a base case, which can also be implemented in an object oriented language with a for loop or a while loop.

In JavaScript, this way of using for/while loops is actually faster than simple recursion. Listen to George explain why that is as he dives deeper into performance in JavaScript.

After learning more about performance, get started on the more difficult recursive challenges to come and really test your overall understanding!

Challenge: getLength

The getLength challenge asks you to get the length of an array using recursion without accessing its length property.

Input: {Array} array - array whose length is sought
Output: {Number}

Though we love JavaScript properties and using short cut ways to find things out, challenging ourselves to know how to access things the long way leads to a greater understanding of how JavaScript works.

getLength is a great example of taking the longer way to find something but learning much more about recursion and JavaScript than use of the length property has ever taught anyone!

Solution: getLength

Similar to George’s explanation videos earlier, CSX has solution videos for multiple challenges throughout the course. These solution videos differ because they’re primarily sudo code and walking through a certain solution line by line rather than going into depth on why that solution is efficient or what that says about the concept or the language as a whole.

These type of solution videos (created by CSX mentors) will show you a specific way to solve the problem, and if you have another solution that works to solve the same problem — awesome! There are many ways to solve every problem, so by no means does having a different solution mean you are incorrect.

At Codesmith we celebrate efficient and creative ways to solve solutions, so if you think you’ve come up with a spectacular solution shoot us an email with it to csx@codesmith.io or slack us in the csx slack!

Now time to watch CSX Mentor, Pete, explain his solution to getLength! Be sure to always complete the problem before watching the solution for all those reasons we outlined earlier ;).

Challenge: POW

POW is a particularly interesting recursion challenge. It requires you to write a function that takes 2 inputs — a base and an exponent — and return the value of the base ^ exponent.

For example if our base is 2 and our exponent is 3, then return 8 because 2³ = 2*2*2 = 8.

Input 1: {Number} base - base number raised to the exponent
Input 2: {Number} exponent - exponent the base is raised to
Output: {Number} - expected value of base raised to exponent

This is another classic recursion problem that will really get at your understanding of this difficult concept!

Challenge: flow

For the final challenge in the CSX recursion unit we’d like you to write a function that takes an array of functions and a number that will be piped through all those functions.

The input number passes through the first function, whose output is passed as input to the second function, whose output is passed as input to the third function, and so on.

We want you to use recursion to return the final output of the last function in the array.

Input 1: {Number} input - number flowing through all functions
Input 2: {Array} funcArray - array of functions to pass input through
Output: {Number} - final output of final function

We’ll have you test your answer with these functions:

const arrayOfFunctions = [multiplyBy2, add7, modulo4, subtract10];

Workshop — Recursion — A Deep Dive (schedule to attend in-person)

The workshop associated with the Recursion unit in the CSX series is Recursion — A Deep Dive.

To compliment what you learned through these CSX videos and challenges in the workshop you’ll take a look under the hood to gain a better understanding of when and how to use this powerful technology.

This workshop will consist of lecture, Q&A, and pair programming. Pairing through the recursion exercises is especially rewarding and best done in person if you’re in LA/NY!

You can RSVP to the next Recursion Workshop on the Codesmith events page here.

All workshops and events are free to attend!

Hope to see you there :)

For more videos, challenges, and solutions like this be sure to make a free account on CSX, Codesmith’s free online JavaScript learning journey!