Four Semesters of Computer Science in 5 Hours Exercise 1 Solution
This course has been updated! We now recommend you take the Complete Intro to Computer Science course.
Transcript from the "Exercise 1 Solution" Lesson
>> Speaker 1: How do we integrate the Jasmine test cases with codepen?
>> 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: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.
>> 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.
>> Brian Holt: I think I fixed all them too. One of them was wrong. Yeah, right there.