Check out a free preview of the full The Good Parts of JavaScript and the Web course
The "Function Challenge 9" Lesson is part of the full, The Good Parts of JavaScript and the Web course featured in this preview video. Here's what you'd learn in this lesson:
In this challenge, you will write the exp() function and gives the instructions for writing the addg() function.
Transcript from the "Function Challenge 9" Lesson
[00:00:00]
>> [MUSIC]
[00:00:03]
>> Douglas Crockford: Write a function exp that evaluates simple array expressions. A simple array expression is an array in which the first element is a function and the remaining elements are the arguments to that function. So if we pass that array, mul, five, 11 to the EXP function, it will return 55.
[00:00:31]
And if we simply pass a number to the EXP function. It will just return that number, okay?
>> Speaker 2: Binary?
>> Douglas Crockford: Yeah, but some are binary function. It turns out it will also work with unary functions, but-
>> Speaker 3: What's the next? Right, exp. Sorry.
>> Douglas Crockford: Yeah. Yeah, you're right.
[00:00:54]
>> Speaker 3: And it's always right?
>> Douglas Crockford: Could be two or three.
>> Speaker 3: Two or three, okay.
>> Douglas Crockford: But you don't care about that. Assume three, it's always.
>> Speaker 3: Sure.
>> Douglas Crockford: Next year it'll be n, but.
>> Speaker 3: Right, okay.
>> Douglas Crockford: But this year it'll be three.
>> Douglas Crockford: Okay, here is exp.
[00:01:17]
Exp takes a value and it returns something if the value is an array. It returns a result of calling the first element, passing the next two elements as arguments. Otherwise, it returns the value. So, who got something that works? Very good. Wanna do one more? You think you've got it in you?
[00:01:38]
It's gonna be based on this one, so if anybody didn't get this one, get it now cuz you're gonna need it. Okay ready? Last problem. Finish Strong OK so. Modify x P. to evaluate nested array expressions and that's that or expression is. Just like the simpler expressions except any of the arguments can also be a nested array expression.
[00:02:08]
So in this case, we've got a hypotenuse thing going on here. And if we pass that nested array structure to exp, it will now evaluate the whole thing and come up with 5.
>> Douglas Crockford: Everybody got it? [BLANK_AUDIO ]
>> Speaker 3: Getting one parameters is an array, can that pass of, pass to if it's evaluated.
[00:02:40]
>> Douglas Crockford: Yes. Exactly. So let's take a look at EXP. So the only change that I made was I called EXP on each of the arguments before we use them. That's it. And so, recursion ends up doing all the work, which is really really nice. Anytime you're dealing with nested data structures Recursion is usually the ideal way to deal with that.
[00:03:06]
So one of the nice things that this function illustrates is just how powerful function systems can be. That this little function in it is a little function Implement most of a programming language. Anyone recognize what language this is? List, yeah in list they use parents instead of brackets and the commas are optional otherwise this is Lisp.
[00:03:34]
It's clearly not the whole language, but it's an interesting part of the language. And one of the reasons why people who do Lisp act the way they do is because they can do stuff like this really, really fast, really, really easily. It's just, you compare that to implementing a system to do one of our languages.
[00:03:56]
And it's just a completely different thing. So I hope you all enjoy this. Today was grueling but you came out, does anybody feel like their brain got bigger today? Anyone feeling that? So before I send you home obviously you're going to need some homework right? So let's get to the homework.
[00:04:15]
The homework problem is write a function add G that adds for many invocations until it sees an empty invocation. When you've got an empty invocation when you receive undefined as an argument. So in this case if you pass nothing to add G, it will return undefined. If you pass two and then nothing.
[00:04:36]
It will return 2. If you pass 2 and then 7 and then nothing, it will return 9. If you pass 3 and then 0, then 4, and then nothing, it will return 7. And if you pass 1 and then 2 and then 4 and then 8 and then nothing, it will return 15.
[00:04:49]
So I'll give you a hint. This problem is going to involve a function returning itself, which is something you may have never encountered before.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops