Functional JavaScript First Steps

# Iteration vs Recursion Exercise

## Anjana Vakil

Software Engineer & Educator

### Check out a free preview of the full Functional JavaScript First Steps course

The "Iteration vs Recursion Exercise" Lesson is part of the full, Functional JavaScript First Steps course featured in this preview video. Here's what you'd learn in this lesson:

Students are instructed to compare iterative and recursive functions for their readability, writability, and performance.

Preview
Close

### Transcript from the "Iteration vs Recursion Exercise" Lesson

[00:00:00]
>> So in the next exercise, we're gonna actually dig into that. And after we've completed these exercises, we will have a moment to discuss all of these great questions about performance and about problems that you might run into when you're working with large data. So the next exercise that we're going to do is in a notebook exercise iteration versus recursion, which you can also again find in that functional JavaScript first steps.

[00:00:35]
Outline right here, exercise iteration versus recursion. And it's also in this collection page, the functional JavaScript first steps, exercise iteration versus recursion. And so the exercises in here are all described. What we're gonna do is we're going to keep in mind what we know about a recursive function and how to write one.

[00:00:59]
And then we're gonna do is compare iteration and recursion in terms of readability. So I got some code for you to read and think about here. Right ability. So we're gonna actually implement some functions and then we're gonna think about performance. So I've got a couple of these exercises that are gonna really help us think about these different, the different mini paradigms for doing this repetition.

[00:01:27]
In the right ability exercise, you are going to write your own implementation of a function. And so really quick I just wanna let you know a couple of things about how observable works. Because that's gonna allow you to do this coding exercise, right in your browser, in this notebook.

[00:01:44]
Again, you do not need an observable account, to do this. However, if you would like to save your work, and save your, functions that you're about to write here today, you definitely can, by signing up for observable and forking, this notebook. And when you do that, it will be saved in your own account.

[00:02:06]
Otherwise, whenever you reload this page, you're going to lose whatever edits you had made to these cells. So just keep that in mind. So the way observable works is when I write some JavaScript in one of these things called a cell. I am going to get a little blue play button, is the little play button on the right is gonna show up blue, whenever I've made edits.

[00:02:44]
And once I'm happy with the edits that I've made, whoops, I'm going to hit that blue button or I can also use the keyboard shortcut Shift Enter to run the code that I've entered. So that is what is going to then. Through the magic, the functional reactive magic of observable is going to update the rest of my notebook with the new version of the function that I've implemented.

[00:03:16]
And once I've implemented this, function successfully I should see all of these little Winkey faces turn to party popper emojis to know that I'm getting the correct outputs. And if I do something wrong, like bad syntax, I'll see some errors. And you might see other errors pop up, because this function is no longer able to be called.

[00:03:38]
But don't worry, you can always Ctrl or Command Z to go back, Shift Enter to re evaluate, or to rerun this little cell, this little function code. And the rest of the notebook will update and instantly if anybody runs into any problems with using observable, please don't hesitate to ask questions and there is also a.

[00:04:03]
Little help button down here with keyboard shortcuts that you can take a look at, if you forget is it shift enter or control enter you can use that little help menu to access that. So that is just a little bit about the environment that we're working in which once again, is just really convenient because we don't have to install anything.

[00:04:24]
We don't have to have the right version of node. We don't have to do any of that. So that's why we're doing it this way.

### Learn Straight from the Experts Who Shape the Modern Web

• In-depth Courses