Transcript from the "Empty Room Solution" Lesson
>> So, our task is to get through this data structure, however, creatively we want and figure out which room, our suspects were not in. So for example, on Miss Scarlett on that night, she was in the conservatory, Reverend Green, he was in the kitchen. Colonel Mustard was in the billiard's room, etcetera, so we want to find out whose was not, which room no one was in.
>> Well we only need the room right?
>> Do we need the room?
>> Just the room.
[00:01:01] So, How'd you guys attempt it? Anyway attempt it?
>> I started by creating a variable, not enroll.
>> And tried using reduce.
>> So we have some not in room function perhaps, maybe we wanna make this fancy. Okay, and then maybe reduce.
[00:01:46] Okay, so for our list probably for reduce, we wanna do new development, and maybe notInRoom is our callback. And so what we wanna take from this is what? What do we want it to return, we want it to return maybe an array of rooms. No living room, Bathroom or something like that, seems reasonable, so we want it to return an array, And then, So if we wanted to return an array we should start with an initial value of an empty array, that's a trick.
[00:02:45] And we wanna make sure that we wanna push that value to our memo, Push whatever it is that we're gonna push, okay? So what's this first thing, maybe like the room.
>> Good catch rooms, so, Really, well, really, this is going to be the suspect. And then we have to get into suspect that rooms, right, because reduce is just going to loop through the top level.
[00:03:47] So the first object and our top level is a suspect, so if we want to get to the rooms we have to reference it like this suspect.rooms. And then we eventually want to push the room where, like, they're not there, no one was there, something like that, okay?
[00:04:16] All right, so what do we have to do next?
>> The way I did it or tried to do it was just take all of the rooms arrays, and then concat them together.
>> And then loop and see.
>> And see you'd have a key and object, no you'd have like a pair for each entry, it would be either room and then true or false.
>> So if the data structure you're created look like what, it was an object?
>> It was an array of objects that had room and then boolean. For each entry.
>> Got it, so it would be like, Billiard's room like that?
>> So it would return true if it was not in the room, so no one was in the Billiards room that would be true, is that what you were thinking?
>> Well, I was gonna start with just the like put all the rays in to get it out of the actually there's no point doing that.
>> First [CROSSTALK]
>> You would have an array of like this?
>> Yeah, okay, so that's another thing we could do is we could and then you can reduce this.
>> [INAUDIBLE] To reduce twice.
>> What's that?
>> Well that might not need to reduce twice and that's a second level thing.
>> So yeah, well, we're gonna have to loop I think we will have to reduce twice. So do some sort of reduced like functionality, because we need to figure out which ones are false here and come up with a list of all of the false ones.
[00:06:13] And then we need to do it for all of them and then we need to do it across all of the suspects, so it's kind of a two-dimensional thing. Okay, All right, so where were we, so we have two approaches. We can reduce this array after we create this data structure.
[00:06:45] Or we can try to all do it in one go. Which would be something like, This is. Do you guys see what I'm doing, so what we wanna do in this reduce is we want to return an array of all the falses. So for example for Miss Scarlet we wanna return the billiard's room, the ballroom and the kitchen.
[00:07:43] And then we want to reduce it again from, So eventually, let's see, so we'll have our, For each suspect we're gonna get an array. And then we need to flatten that array. No then, we need to make a opposite unique. So if there's a duplicate we need to, we need to remove it does that make sense?
[00:08:24] So, the first time we reduce it we're gonna get, Kitchen, Ballroom, And billiard room okay, for Miss Scarlett. And then the next person it's gonna be something like this. Ballroom conservatory, dining room there's some more rooms. Let's just say, So, when we reduce these two. Well, what we really wanna do is compare between these two data structures and see.
[00:09:38] So if these two are shared, we wanna keep it. If this one's not shared we don't want to keep it because that means that this person reverend Green had been in the bill your term anthems I guess following okay so. Getting from here we need to somehow we need to fight there's we can use a difference.
[00:10:16] We can do the difference of the arrays. So I believe, There is, Something called difference. Let's check out the documentation. All right, so creates an array of array values not included in the other blah, blah, perfect. So that's what we'll do. We'll use difference for those, okay. So maybe we don't need to reduce so many times after all.
[00:10:56] Yeah, that was gonna get a little crazy. So let's call this our, So we're gonna get multiple arrays and I'm going to store it. We can store it in an object or we can store it in an array and I'm just thinking what is gonna be the best method of doing that?
[00:11:25] I'll just put it in anyway. Why not, okay. So, as we reduce Rooms. Not rooms so these are all the this let's call these the empty rooms. Okay, Okay. So we'll reduce it. This is going to return an array. Hold on a second. There we go. Room, memo.
[00:12:26] Suspect.rooms. Then we need to loop through this, okay? All right, let's just do one thing at a time. So, we wanna say if it's false, so if suspect rooms is false. Memo.push, Sorry, we wanna say room here, push the room and then you wanna return memo. Okay, so what we're doing here is we're looping through the suspect.rooms.
[00:13:22] For each room we're seeing if the value is false if it is we're gonna push it into our memo which is an array because we initialize it as an array. So this is for one suspect. So at the end of this, we're gonna create this empty rooms variable.
[00:13:45] Okay, but we need to do that for multiple people. Okay. It seemed a lot simpler in my head when I wrote that exercise. Okay, so let's just map this. Do we wanna map? So we want it to return. Yeah, so we want to map it. We map. The new development, Over not in room, And this is going to be our, Not in rooms so that this is going to loop over a new development.
[00:14:52] It's going to pass our suspect object into this function, okay? And then we're going to reduce the suspect.rooms, which is an array this is gonna return an array of all the empty rooms. So map is going to be an array of arrays of all the empty rooms.
>> You can do it one more time.
>> Yeah let's yeah, okay. So new development. Let me just put a word wrap here. Okay, so new development, right? Is an array of objects. We're gonna map over it. So this we're looping through. And we wanna map because we want the array to be the same length as the array that we're passing, yeah.
[00:15:58] So here's our first item that's gonna get passed into not in room. That's our suspect. We're gonna reduce the suspect rooms, right? So, we're digging into the suspect rooms. We're gonna pass it, the room, right? That's just how reduce works and the memo. We're gonna initialize it as an empty array.
[00:16:22] So, every time we loop through for each room if it's false, we're gonna push a new room into memo and return it. So, the first time it finds an empty room, it's gonna push kitchen. It's gonna return memo, which is an array. It's gonna have an array with kitchen in it.
[00:16:43] If it's true, it's just gonna return memo. Anyway, it's not gonna add anything to it though So, it's like a fancy filter. Do you guys see how that works? Okay, and then we're gonna return that array. So, that would look, something like this. And then map is gonna start looking something like this.
[00:17:16] And then the second time it goes through, looks something like this. And then we'll have this data structure, and then we want to difference it, and that will give us the rooms that one has been within. Does that makes sense? Then we would say difference not in rooms, and that will give us our solution.
>> How does it know that it's kind of return memo? It's got that empty array. How does it know to push memo into that was pushing?
>> Yeah, this push?
>> It's gonna be pushed into this empty array. Because this is the initial value, so for reduce, you can pass nothing as the initial value and it will choose the first item in the array.
[00:18:39] Or you can give it your own initial value. And you can define the behavior in the callback But a simpler version would really be to use just a filter. But filter is just a type of reduce.
>> So, what difference give in this example? Give billiard room as the difference.
[00:19:14] So, then kitchen and ballroom would still be valid options as the no room.
>> Good point. Maybe I did it backwards, hold on.
>> Yeah, we don't want the difference, we want the not difference. Create an array of values not included.
>> You'd want the room that not shows up the most, but shows up exactly the same number of times as there are suspects, right?
>> Yeah, exactly. So, we can negate difference, let's see. Creates a function that negates the results of the predicate function. So, is even filtering the gate, is even overturn odd. Yeah, maybe if we do a difference and we negate it. To make it more complicated, why not? Wait, but this is not a function.
[00:20:26] So maybe,
>> We've been negating the difference, right?
>> Well, It has to be a predicate. I don't know that that will work. Maybe there's a deep unique. Let's see, now, Probably one of these ones. 3, 1 and 3. I really wanna use, I can do it logically even code but I really wanna do, I wanna use a low dash method.
>> So, like an array intersection or something?
>> Yeah, sounds looking, what is this?
>> Yep. There's an underscore, a low dash intersection.
>> Let's do that. I knew there had to be something. There we go, and now we want, it would return this. All right.
[00:22:15] What do you think? Well, Krasny Any questions? Yeah, over the head. I mean, it's pretty much over my head too. Okay, Thinking about this, So, we can also easily use a filter here to do the same thing as this reduces doing. Cool. All right, now, you guys are like I never wanna use reduce again.
[00:23:33] That's what they say, people who criticize reduce say, it's too complicated. So, the only thing that people can do with it is add numbers. But the people who are really into it are like really into it. I like it just for the riddles, like I said at the beginning of the class I really like riddles, and mysteries and kind of like figuring stuff out that's what I like about.
[00:24:05] It's kind of is like a riddle for me how to use reduce.