This course has been updated! We now recommend you take the Complete Intro to Computer Science course.

Check out a free preview of the full Four Semesters of Computer Science in 5 Hours course:
The "Exercise 1 Solution" Lesson is part of the full, Four Semesters of Computer Science in 5 Hours course featured in this preview video. Here's what you'd learn in this lesson:

- Brian walks through the solution to exercise 1. He also demonstrates how to include unit testing libraries in Codepen.

Get Unlimited Access Now

Transcript from the "Exercise 1 Solution" Lesson

[00:00:00]
>> [MUSIC]

[00:00:03]
>> Speaker 1: How do we integrate the Jasmine test cases with codepen?
>> Brian Holt: Good question. Let me just briefly show you since it's kind of outside the scope of the class but if you click into the codepen, then you click Settings and you click JavaScript. Notice I'm just including a bunch of Jasmine files and I just took that off of someone's blog post.

[00:00:23] In fact you can probably just search for a codepen Jasmine and it'll be up there. There's also a CSS file too, but it's just a bunch of external link included JavaScript.
>> Brian Holt: Okay let's work through this together. So here we're gonna put our code. Have my notes here, and I'm gonna make a function called factorial, and it's gonna take an (n).

[00:00:54] Another interesting thing to note about CodePen, is if you see this red exclamation point down here, it means that it's not compiling. So if you click on that it's gonna start yelling at you unexpected identifier it's gonna tell you where it's having issues in this particular case we don't have curly braces so it's man but since we put a curly braces that disappears.

[00:01:16] The other thing that's worth mentioning is I did turn on Babel. So again coming here to Settings looking at JavaScript everything's being run through Babel so you're free to use all of ES6 except generators. We're not using generators and that's okay, but just say know codepen has problems with generators so don't even try.

[00:01:37] Okay, so the first line of our recursion, what's the first thing we're gonna do? Base case, right? So if (n < 2), then we're going to return 1, pretty simple, okay? And now what is a factorial? Well, it's n * factorial of (n- 1).
>> Brian Holt: That's it and if you look down here, you noticed that our test is now passing.

[00:02:22]
>> Brian Holt: So again to just to demonstrate here it's factorial if you think about It's whatever the number is times whatever the factorial of the number of minus one, right? And that's literally what we're expressing into the code, right? It's n times factorial of (n- 1). So that's something that can really help you is just to reason allowed of what's happening.

[00:02:48] This is helpful for me and it's confusing for other people. Some I'm gonna to state it and if it makes you more confused just throw it away. You can kind of think of the body of the function of of a recursive function like the inner part of the loop, right?

[00:03:01] That's a very leaky abstractions that don't go too far with that that just think of that, that's the code that gets run over and over and over again. Just if you do a for loop, whatever the codes in the middle of the for loop that's what gets run over and over and over again.

[00:03:13] This is the same general concept. This is the code that's gonna get run a bunch of times. Maybe that helps you maybe doesn't so any questions? So, let's close that. Leave this page, leave that page. And again, all the answers are up here, so if you're struggling or you wanna go back later and look at it, all the answers that I'm putting up here, they have them right here as well.

[00:03:46]
>> Brian Holt: I think I fixed all them too. One of them was wrong. Yeah, right there.