This course has been updated! We now recommend you take the JavaScript: The Recent Parts course.

Check out a free preview of the full ES6: The Right Parts course:
The "Exercise 4 Solution" Lesson is part of the full, ES6: The Right Parts course featured in this preview video. Here's what you'd learn in this lesson:

Kyle walks through the solution to exercise 4. The solution is located in the file ex4-fixed.js.

Get Unlimited Access Now

Transcript from the "Exercise 4 Solution" Lesson

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

[00:00:03]
>> Kyle Simpson: So let's talk about Exercise 4. Let's start from the bottom. Let's say that we wanna tag our strings so we want to put upper in front of it. Our expressions need to look like this. So we're going to say ${name}. And then ${twitter}. And then $, is it {classname}?

[00:00:31] Yeah. So there's our string literal. It needs to match this particular string, so you can see it needs to be able to pull in those and uppercase the values. So let's do that, let's write a tag function to do that. We start out with an empty string. We do a for loop.

[00:01:01]
>> Kyle Simpson: We say if (i > 0) meaning I'm dealing with the the values, I wanna add, concatenate onto the string values of [i- 1] but I want to call toUpperCase on it. And then I want to add it strings of i and return my string. Let's test it and see if that worked.

[00:01:35]
>> Kyle Simpson: And we got true.
>> Kyle Simpson: So there's our tag function.
>> Kyle Simpson: Questions about exercise four?
>> Kyle Simpson: Yes.
>> Audience member: I was trying to use map and sort of a for loop but I couldn't get it to work.
>> Kyle Simpson: Okay so map, this isn't the functional class but since you brought it up, map is for taking a list of things going to another list of things.

[00:02:08] That's not quite what we wanna do here. We wanna go from a list of things down to one concrete value which is the reduce operator. And you could have done this with reduce if you wanted to. The only trick with doing this with reduce that's one of those cases because reduce isn't built to go over two lists and we're technically traversing two lists here.

[00:02:27] If you did it that way you'd technically be doing an impure reducer. So you probably wanna wrap that up in some special pure utility. If you really wanna be like, if you don't want the FP police to come after you.
>> Kyle Simpson: But you can do it with Redux.

[00:02:53]
>> Kyle Simpson: [COUGH]
>> Kyle Simpson: Other questions?
>> Kyle Simpson: You may not author a lot of these yourself. You might wanna just tweak them maybe every once in a while. But I would definitely recommend go search on GitHub, you'll find several different repos of libraries where people have collected together useful preexisting ones.

[00:03:16] You don't need to reinvent your own logic. Several good ones out there.