Check out a free preview of the full JavaScript: From Fundamentals to Functional JS, v2 course:
The "Looping Solution, Part 2" Lesson is part of the full, JavaScript: From Fundamentals to Functional JS, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Bianca walks through the solution with the help of students. Bianca takes questions from students.

Get Unlimited Access Now

Transcript from the "Looping Solution, Part 2" Lesson

[00:00:00]
>> So the exercise was to loop through all the properties of the suspect objects in the suspects array and mark them if you think you're guilty. I'm curious to see how you guys approached it. So we still have this game here. Do you want to take a step, did you finish it?

[00:00:26]
>> Mostly I use for each method on the array.
>> You use for each? Could anyone use a loop? Use loop?
>> It's a foreign like you're shown here.
>> Yeah cool we're gonna do for nginx though. You're ahead of the game. All right, so walk me through it.

[00:00:43]
>> Yeah, sure. So I just declared a function called game loop. So var gameLoop equals function And the first line of that is a for loop, for var i equals 0. I less than games.suspects.length? i plus plus.
>> Length, you guys had to be my spellcheck here, okay?

[00:01:15]
>> I did the for in loops I said four key in var game.suspects[i] first learned that is if statement. I didn't really know exactly how to figure out who's guilty. I was thinking of getting a like the confidential object. But for now I thought rusty was it so I just said if game.suspects[i] and then key because i noticed you can't use .key here for whatever reason that didn't work maybe you can explain but I had to do this.

[00:01:57] So if that all == to really rough rusty, I just said rusty in string. Then I would have a log, says found one and or return true maybe but I just did a log there, found.
>> Can't type.
>> Yeah, and then else not this time or something like that.

[00:02:36]
>> All right.
>> So since we have one of the things matching that should return one, found him in the rest next times.
>> Cool. I like it. How many times does this outer loop run? I feel like I pick on the people on the outside the most.

[00:03:04] Actually I haven't taught to our friend in the back. How many times do this this loop run?
>> It runs through each entry.
>> For each what?
>> It runs for each entry in suspects.
>> Yeah, so each suspect object, right? So in number form, two times, right?

[00:03:27] Cool. How many times, Does this loop run?
>> Four.
>> Yeah, and what order does it run in? So if I was going to console.log and I'm gonna try to do this quick, outer loop. Then we console.log, inner loop, so what's the first thing that I will log?

[00:04:04] How about combat, sorry, [LAUGH]
>> Outer loop?
>> Yep. So it'll log outer loop. Okay then our lawyer friend, what's next thing that I'll log?
>> Inner loop.
>> Okay.
>> And then another inner loop.
>> Then two more inner loops.
>> Not quite, so at this point, our first inner loop suspect is gonna be our very first one, right, it's gonna be rusty orange.

[00:04:50] So for all the keys in this object, it's going to run it twice. So the first one is name, right? Next one is color and then that's it. So then we break out of this loop and we continue back here. Assuming that we didn't hit this if else statement which we would have.

[00:05:18] That makes sense, why it doesn't keep looping?
>> Not really, you can just probably go.
>> Yeah, sure, one more time. So do you know what this object is?
>> Yeah, it's the first key in suspects, I mean, rusty in orange.
>> Yeah, so, and it's going to loop through this object.

[00:05:45] So there are two properties in this object, right? There is name and then there's color. So it's gonna loop through both of those, and that's all it knows about it because i0 and we've only made it that far so the loop doesn't like, jump ahead. It doesn't know that, in fact, we're gonna ask it to loop again for the next one, but it won't know that until we say game.suspects at one.

[00:06:14] See what I mean is that clarify? Yeah, cool. So we have outer loop, inner loop, inner loop, and then this corner over here how about in the end? What's the next thing that's going to log? You know?
>> Outer loop?
>> Not quite sense suspects at i is rusty.

[00:06:47] So what's the next thing?
>> It's rusty and it's gonna print found them?
>> Yeah, awesome. And then what's the next thing that's gonna run? Next door. What's the next log?
>> Me?
>> [INAUDIBLE] Please.
>> You fulfill the logic when you found roastery.
>> When you say we fulfill the logic what does that mean?

[00:07:20]
>> Like it was equal to us so you console log you found so you resumed on here but then you go if. Then you iterate again, you kind of go through that process and catch the else.
>> Well what's the very next thing that happens after this? So you're right that we met a condition for our if statement we entered into an if block, and then you know we're gonna move on from.

[00:07:48]
>> You probably gonna go back to the outer loop. Yeah.
>> And repeat the operation.
>> So console.log outer loop next And then next,
>> Inner loop twice more.
>> Yep, we"ll do inner loop.
>> Shouldn't found them actually be after the first inner loop, Matching the name?

[00:08:16] Yeah, you mean like this one should be later? Well, I mean, there's no guaranteed but we'll just pretend that its name just for fun. So then it's going to be inner loop. What's that?
>> There's no guarantee on the order of the propeties.
>> No we'll just pretend

[00:08:34]
>> And then we're gonna enter into the if else. What do you think is gonna log? We're here.
>> Why is it running again through it? Why does it stop after under console.log ('Found \'em !') why would it go?
>> Where did it tell it to stop?
>> Because it's just locked it to the grids.

[00:08:59]
>> How could you tell it to stop, do you know?
>> Break? Break?
>> Yeah, you can break you could return stuff like that. Cool.
>> Wouldn't lock. So it's just gonna be rusty again.
>> Well the color is orange, yeah, it's gonna be the next property. So then it'll say next time.

[00:09:21]
>> Okay.
>> That makes sense?
>> It's not rusty.
>> Yeah.
>> That has to be.
>> Yeah, cool. You guys see where this is going and how this flows? Okay.
>> Can we run out once just to see if we're right?
>> Yeah. We need this Just because I don't want to run into more issues with that game has already been conflict declared.

[00:10:08] All right, Okay, so, game loop. What did I say game 23. This is a problem. You guys are supposed to be my spell check. Okay, all right, no, I think we're ready, let's see. So we have outer loop, inner loop, found ' inner loop. Should we look at it side by side maybe, All right, let's see the outer loop, inner loop found '.

[00:11:13] We didn't make a mistake.
>> Let me go through inner loop again. And then next time.
>> How do we get there? Okay thought that's what we did, well. And then two inner loops, next times.
>> Yeah, why don't go through all of them?
>> Was going back through both of the properties.

[00:11:39]
>> The part we missed was the inner loop going through multiple properties?
>> Yeah, well, the second one we didn't finish the second one. But yeah, I think we.
>> Inner loops is gonna run twice
>> Yeah
>> It's going through two different keys
>> Yeah, and it's also gonna be hitting these this Fs which will be logging as well.

[00:12:01] Cool.