This course has been updated! We now recommend you take the The Good Parts of JavaScript and the Web course.
Transcript from the "Problems 1-5" Lesson
[00:00:00]
>> [MUSIC]
[00:00:04]
>> Douglas Crockford: All right, so I've got problems, now they're gonna be your problems. So what we're gonna do for the next hour two is I'm gonna show you a problem statement, and you can ask as many questions as you want about it. And then you'll solve it, you'll write down or type it in, or whatever.
[00:00:23] Then I'll show you my solution, you can compare your solution to mine and we'll talk about that. Each of these will build on the previous ones, so if you didn't get one, then right down my solution because you're gonna need that to do the next one, okay? The plan is after doing this, you're gonna be about 100 times smarter about functions, okay?
[00:00:44] We'll see how that goes.
>> Douglas Crockford: And I promise you, we'll start easy, and then it'll get hard, and hard, and hard, and hard, right? So first, easy one. We'll start with a test, okay. Someone asked if there is gonna be a quiz. And there is, and it's right now.
[00:01:01]
>> Speaker 2: [LAUGH]
>> Douglas Crockford: OK, so we have function funky. Has a parameter (o), it sets o to null. We've got a global variable x, which is an empty array. And we call funky (x), and then we do alert. What does alert tell us? A, that it's null, B, that it's an empty array, C, that it's undefined or D, it throws?
[00:01:24] Okay, so who thinks the value of x is null? Who thinks it's an empty array?
>> Douglas Crockford: Who thinks it's undefined? Who thinks it throws? Who didn't raised their hand? [LAUGH] It's the most of you. Okay, it turns out the correct answer is B.
>> [SOUND]
>> Douglas Crockford: And the reason for that is that o is a parameter.
[00:01:51] O knows nothing about B, o only knows the value that was passed to it, and the value of B was the array. Okay, so it takes it parameter, changes it to null. That does not change B. It does not change the array, it just changes what o was pointing to, okay?
[00:02:11] Okay, here's the next one. We have function swap that takes a and b, and using a temp variable, switches a and b. We've got x = 1, y = 2, swap (x, y), what is the value of x? Who thinks it's 1?
>> Douglas Crockford: Who thinks it's 2? Who thinks it's undefined?
[00:02:35] Who thinks it throws? And who didn't raise their hand? Okay, the answer is 1, for exactly the same reason. If swap were a macro, then the right answer would have been 2, but it's not a macro, it's a function. And again, it doesn't see the variables that were passed.
[00:02:53] It only sees the value that those variables were bound to. So it's just wasting time. It's not actually accomplishing anything, does not change any of the global state. Everybody with me? All right, so now to the problems. Now you're gonna start to work, and I get to watch you work.
[00:03:14] Okay, so the first one is real easy. Write a function that takes an argument and returns that argument.
>> Douglas Crockford: Okay, so how am I gonna know when you're all done? Maybe everybody should look attentive.
>> Douglas Crockford: Anyone need more time?
>> Douglas Crockford: Okay, so here's the solution, the function identity (x) {return x}.
[00:03:43] So pretty close to the simplest possible function you can write. It's also, too, correct to have said var identity equals a function expression. These two both do the same thing.
>> Douglas Crockford: All right, any arguments about this one? Okay, everybody know how? Cuz you gonna need this one later.
[00:04:05] Okay, so let's move on. Another easy one. Write two binary functions, add and mul, which take two numbers and return the sum or the product. It really is as easy as it looks.
>> Douglas Crockford: There's no trick here.
>> Douglas Crockford: We talked earlier about the problems with concatenation. Don't worry about that here.
[00:04:30] Let's just assume that plus adds, and that's good enough.
>> Douglas Crockford: Does anyone need more time?
>> Douglas Crockford: All right, so here it is. Function add (x, y) {x + y}, function mul (x, y) {x * y}, right. Everybody should have gotten these. The reason for these was we're going to use them later, but I wanted you to write them, then not have to assume anything.
[00:04:57] Okay, here comes the first interesting one, you ready? Write a function that takes an argument and returns a function that returns that argument. Okay, so we have identityf takes a 3, returns a function, we call that function, it returns 3.
>> Douglas Crockford: Does anyone need more time?
>> Douglas Crockford: I can't tell who's working and who's surfing for porn, so it's hard to-
[00:05:27]
>> Speaker 2: [LAUGH]
>> Douglas Crockford: Hard to tell who's finished. If you need more time please, don't be shy, let me know, but I'm assuming that we're all ready for the reveal? Okay, so we have identityf (x), it returns the function that returns y. Who got this one? Really good, anyone who didn't, does this make sense to you?
[00:05:56]
>> Douglas Crockford: Cuz this is the key, everything we're gonna do for the rest of the day is going to look like this. So I need to know that everybody understands this one before we go on.
>> Douglas Crockford: Is there any questions?
>> Douglas Crockford: No, all right, here's the next one. Write a function that adds from two invocations.
[00:06:19] So you're gonna write function addf that will take a 3, and then take a 4, and return 7.
>> Douglas Crockford: And I'll give you a hint, you'll combine elements of your add function with elements of the identity f function.
>> Speaker 3: I have a question, what happens when I invoke it for a third time time?
[00:06:42]
>> Douglas Crockford: That should be an error, because you're trying to invoke a number.
>> Douglas Crockford: Because the result of the first two indications will be 7, so trying to call 7 should be a type error.
>> Douglas Crockford: Does anyone need more time?
>> Douglas Crockford: Okay, so here we go, function addf (x) { return function (y) { return x + y}.
[00:07:14] So who got this one?
>> Speaker 4: [INAUDIBLE]
>> Douglas Crockford: Okay, I saw you shook your head, how-
>> Speaker 4: I didn't get that.
>> Douglas Crockford: What did you miss?
>> Speaker 4: I was going a bit more complex. [LAUGH]
>> Douglas Crockford: That's later.
>> Speaker 4: I'm all ready for you.
>> Douglas Crockford: [LAUGH] Anyone else have a question about this one?
[00:07:37] Even if you didn't get it, I recommend you write it down, because these are all going to build on each other, and it may be useful to have this to refer back to. Okay, so ready for the next one?
>> Douglas Crockford: Write a function that takes a binary function, like add, and makes it callable with two invocations.
[00:08:01] Okay, so you are gonna write a function called applyf, you can (add), and then (3) and then (4), and get 7.
>> Douglas Crockford: Or we could call applyf with (mul) (5) (6) // 30.
>> Douglas Crockford: So this is all gonna be about higher order functions. In the first functions, we saw functions returning functions.
[00:08:27] Now we have functions doing that, but also taking functions as parameters.
>> Douglas Crockford: You seem to be smiling very pleasantly.
>> Speaker 4: No, it's just crazy.
>> Douglas Crockford: [LAUGH] Why would you do that? [LAUGH]
>> Speaker 4: Make the card readable, I think. [LAUGH].
>> Douglas Crockford: No, we're actually gonna build towards stuff which is useful.
[00:08:52] But first you need to get the fundamentals.
>> Douglas Crockford: Does anyone need more time? I keep asking that, and no one has given me an indication. I don't know that my intuition is, it's all right, you do. Good, thank you, okay.
>> Douglas Crockford: All right, so here it is, we have function applyf (binary) {return function (x) {return function (y) {return binary (x, y) }.
[00:09:37] So who got this one? Really good, really good.
>> Speaker 4: I have one question, though.
>> Douglas Crockford: Yeah.
>> Speaker 4: In the applyf, I'm first defining a variable, it says store function equals binary. And I'm returning store function x, y. I can't really see how that's different.
>> Douglas Crockford: Yeah, it's the same thing.
[00:09:56] Yours is doing more work than it has to, but this is a little simpler.
>> Speaker 5: It's sort of using your closure thing to collect all this stuff and to keep track of it.
>> Douglas Crockford: Yeah, so the closure is holding all the stuff. So everything that the blue function needs in order to do its job, it gets all of that from the closure now going several levels.
[00:10:21]
>> Douglas Crockford: Anybody have any questions about this one? Again, this is key and we're gonna be building more of this stuff so,