Check out a free preview of the full Introduction to JavaScript course
The "Testing Solution with Objects" Lesson is part of the full, Introduction to JavaScript course featured in this preview video. Here's what you'd learn in this lesson:
Brian live codes the two object array sections of the solution.
Transcript from the "Testing Solution with Objects" Lesson
[00:00:00]
>> Brian Holt: Great, so let's do Average Age here. So, I'll show you a couple ways to do this, let's start with the for loop way and then I'll show you the extra clever way of doing it.
>> Brian Holt: I is list.length i++, okay? Here we're gonna have some sort of sum, sort of say, let sum and we're gonna start that at zero, okay?
[00:00:31]
And then what we're gonna do here is we're just gonna say, sum += list[i].age, right? Relatively similar what were doing up here, we're pulling the person off and then putting the name on. Here we're doing the same thing here, that we're grabbing the age off of the person and we're adding that to the current running sum, right?
[00:00:53]
So by the end of this for-loop, I'm going to have the current.
>> Brian Holt: The sum of all the ages of all the workers, right? You with me so far? Okay, then here, the average, right, is the entire sum divided by how many items are in the list, right?
[00:01:15]
So I'm gonna say, sum divided by list.length
>> Brian Holt: Right, make sense?
>> Brian Holt: So and that does end up solving it. Remember how I told you that JavaScript numbers are quite imprecise particularly when it comes to matters of division. Just to know that code down here at the bottom here, I think its this a function that actually has like a tolerance built into it, where is it?
[00:01:50]
It is down here, all the way down here, right there, to be close to, right? So as long as you're within, I think like a whole tenth, it'll still count it as right.
>> Brian Holt: Okay, where were we? So how do we feel about this? Does average age look okay to you?
[00:02:22]
And I guess I could show you other ways of doing this, but they're gonna look relatively similar to the one above it. So, it's relatively the same game here, as long as you're adding everything together, and then dividing by how many items on the list you're gonna get the average.
[00:02:33]
And that's all I want you to do here, good, okay.
>> Speaker 2: Is there like a built in function that will, just like some numbers together?
>> Brian Holt: Some sort of like aggregator?
>> Speaker 2: Yeah, I tried to find, we can add function or a sum function didn't look like it.
[00:02:52]
>> Brian Holt: No, there's always gonna be anything built in. There's one that you can use called reduce, that's gonna get you most of the way there, and then-
>> Speaker 2: I use that but I still had to write and add function myself.
>> Brian Holt: Yeah, you still have to write it with reducer.
[00:03:05]
But that I mean, it's pretty simple. That would just look like,
>> Speaker 2: That just looks so much simpler than what I did.
>> Brian Holt: This does?
>> Speaker 2: Yeah.
>> Brian Holt: Yeah, if your gonna write this with a reducer you'd say const sum= list.reduce. And you're gonna have some sort of function that's going to basically add all those things together.
[00:03:25]
So it's gonna take in some sort of like running sum and a current person, current person. And then it's going to do a reduce which is going to return the runningSum+current person.age, and you have to start with 0. So that's how it'll look if you try to, this is a quite a bit more advanced, so I wouldn't worry too much if this doesn't make a ton of sense, but that's how you would do it in terms of reduce.
[00:04:01]
This will give you the sum and then here you'd still have to divide by the link at the end.
>> Brian Holt: But if you ask me like, this is clever and such, right? But this is probably a little bit more clear, right? Most people can read that pretty easily.
[00:04:18]
Like I would have to look at that, what the hell is this person doing? [LAUGH] Right? This is over-engineering the problem here. I say that as someone that absolutely loves functional programming and uses reduce everywhere, so I probably would still try and write that. [LAUGH] But I think this is a little bit more clear, cool.
[00:04:40]
Any other questions before I move on?
>> Brian Holt: Cool, so getAllEngineers(list) that are not going to be from Assassin's Creed. Const, so engineers is gonna be this. So, couple ways we could do this. Let's start with a for loop I think, right? I wanna say, for let i equals zero i is less than list.length i++, okay.
[00:05:18]
And then we're gonna do a check here to say, if list i, let's just pull list i out. So const person equals list i. So if person.name.includes which I showed you yesterday, right? Engineer, then you wanna add them to that list, right. You're gonna say, engineers.push(person.name), and this is gonna give you a list of all the engineers.
[00:05:57]
>> Speaker 2: Should be whether their job title as engineer.
>> Brian Holt: Yep, thank you. Job title, I mean, maybe some of them are named engineer. I don't discriminate, but that's true, thank you, cool. And then here you're gonna return engineers.
>> Brian Holt: Yep, good?
>> Speaker 2: One little more question.
>> Brian Holt: Sure, of course.
[00:06:31]
>> Speaker 2: So I used the for each for this, for a problem like this, is it generally better to use a for loop or for each? Does that basically the same thing.
>> Brian Holt: So I'll tell you what I think, but what's actually more important is what you think. What you think is more clear, like what you think you're gonna come to later and it's gonna take you a shorter amount of time and less cognitive to understand.
[00:06:55]
And that's the correct answer to that question. How I would do this in terms of filter and then map. So, I think in general this probably would be, I would choose either filter and map. Which I'll show you how to do here or I would choose a for loop.
[00:07:17]
Const engineers is gonna be,
>> Brian Holt: Equals list.filters, so the first thing I'm gonna do here is I'm gonna filter out everyone that doesn't have engineer in their title. So this is gonna be a person, and here I'm going to return if person.jobtitle. Dot includes engineer, so if you remember I did show you a filter yesterday.
[00:07:49]
What filter does is if you return true in this function, it'll keep that item in the list, right, cuz this function right here is going to be run, oops. On literally every item in this list, right, just like map, just like for each, right? And if it returns true, it'll keep that item in the list.
[00:08:06]
If it returns false, it's gonna kick it out of the list, right? So now I'm gonna have a list of people, right? People objects, so I'm still not quite there, right? But I'm gonna have a bunch of people objects and all of them are gonna have engineer and their title.
[00:08:18]
So what do I have to do after that, I just have to map over these and just return the names. So I'm gonna say, let's put this on a new line.
>> Brian Holt: Then next thing here I would say dot map, person. And then I would return person.name, just like I had done up above.
[00:08:39]
In fact, I could even use that function If you really wanted to, we can.
>> Speaker 3: Person there is just a place holder or is it. Before you had what, list.i, and you called that person is not,
>> Speaker 3: You're not using that constant below, are you?
>> Brian Holt: No, so this is scoped to this for loop right here.
[00:09:12]
So I am, no, I'm overriding engineers here, right? So that's not kosher, but yeah, this this would accomplish the same thing. In fact, you could make this even more succinct by doing name here and just doing name and then you can also do here. Job title, get rid of this, I could say job title.includes,
[00:09:46]
>> Brian Holt: Engineer.
>> Brian Holt: So that does literally the same thing. So, as you learn more and more of these, right, I do absolutely do not expect you to understand right now how this works or to have arrived at a solution like that. But I also want you to be aware of like these things that are out there, right?
[00:10:11]
What's more important to me is that you can arrive at this solution, right? But you're gonna see things like this, which is why I hope to expose you to that. To me, seeing stuff like that, that's really cool I wanna learn how that works, right. So it kind of like picks your interest to kind of go down that path.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops