All language subtitles for pragstudio-ruby-17-hashes (Transcribed on 27-Apr-2023 20-38-54)

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
ny Chichewa
zh-CN Chinese (Simplified) Download
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
tl Filipino
fi Finnish
fr French
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:00,000 --> 00:00:06,920 Now as the game stands now, you're sort of teasing the player, right? 2 00:00:06,920 --> 00:00:10,680 They can find a treasure, but they don't actually get to keep it. 3 00:00:10,680 --> 00:00:13,500 We aren't storing the treasure for each player. 4 00:00:13,500 --> 00:00:17,160 In the same way when we play a movie, we're picking a random snack, but we're not actually 5 00:00:17,160 --> 00:00:19,460 storing that snack with the movie. 6 00:00:19,460 --> 00:00:24,420 So in this section, we want to use a hash to store the snack name and then accumulate 7 00:00:24,420 --> 00:00:26,960 the total carbs for that snack. 8 00:00:26,960 --> 00:00:30,320 Now hash is an index collection of object references. 9 00:00:30,320 --> 00:00:35,400 You'll sometimes hear it called a dictionary, map, or an associative array. 10 00:00:35,400 --> 00:00:38,280 It's useful when you want to keep track of two things. 11 00:00:38,280 --> 00:00:41,720 You can think of it as a two-column table with keys and values. 12 00:00:41,720 --> 00:00:46,019 For example, let's say we want to keep track of snack names and their associated carbs. 13 00:00:46,019 --> 00:00:50,920 The key is the name of the snack and the value is the number of carbs. 14 00:00:50,920 --> 00:00:53,220 The key and the value make a pair. 15 00:00:53,220 --> 00:01:00,440 So we could create the hash like this, snack carbs equals our keys become symbols that 16 00:01:00,440 --> 00:01:05,400 are associated with values and we surround it with curly braces. 17 00:01:05,400 --> 00:01:10,160 Then to get a value, we would index the hash with the values associated key. 18 00:01:10,160 --> 00:01:12,640 Mike, can we look at this hash in IRB? 19 00:01:12,640 --> 00:01:13,640 Sure. 20 00:01:13,640 --> 00:01:18,200 All right, let's just put our hash in a variable called snack carbs, like that, and then we'll 21 00:01:18,200 --> 00:01:19,720 start our hash here. 22 00:01:19,720 --> 00:01:25,000 And then we've got three different types of snacks. 23 00:01:25,000 --> 00:01:30,080 So we've got candy, we'll use a symbol as the key because they work really well as keys. 24 00:01:30,080 --> 00:01:31,880 It's got carbs of 15. 25 00:01:31,880 --> 00:01:37,440 Pretzel has 10 and we've got soda and it has five. 26 00:01:37,440 --> 00:01:38,780 So there we have our hash. 27 00:01:38,780 --> 00:01:44,039 Now to get a particular value, as Nicole said, we just take our hash and then we pass in 28 00:01:44,039 --> 00:01:46,680 the key or we index to the key. 29 00:01:46,680 --> 00:01:50,240 So candy has 15. 30 00:01:50,240 --> 00:01:53,760 Pretzel has 10, just like that. 31 00:01:53,760 --> 00:01:59,480 In Ruby 1.9, if you use symbols as your keys, there's a slightly different syntax that you 32 00:01:59,480 --> 00:02:00,680 can use. 33 00:02:00,680 --> 00:02:08,160 So in Ruby 1.9, we can create our hash like this, snack carbs, same hash literal there, 34 00:02:08,160 --> 00:02:12,240 but because we're using symbols, if we want to use symbols, we can just do this. 35 00:02:12,240 --> 00:02:20,040 Candy is 30, pretzel is 100, and soda is 50. 36 00:02:20,040 --> 00:02:26,720 So you've got the key, candy, like that, and then you just put a colon and you put the 37 00:02:26,720 --> 00:02:27,720 value. 38 00:02:27,720 --> 00:02:29,680 So you can use either of these syntaxes. 39 00:02:29,680 --> 00:02:33,360 You can use the equal and greater than sign or you can do it just like this if you're 40 00:02:33,360 --> 00:02:35,740 using Ruby 1.9. 41 00:02:35,740 --> 00:02:39,840 So we started with a hash that already had keys and values inside of that. 42 00:02:39,840 --> 00:02:40,840 Let's turn it around. 43 00:02:40,840 --> 00:02:41,840 We'll start with an empty hash. 44 00:02:41,840 --> 00:02:47,720 We can either do that by creating an empty hash that way or we could use snack carbs 45 00:02:47,720 --> 00:02:49,900 equals hash.new. 46 00:02:49,900 --> 00:02:50,900 Exact same thing. 47 00:02:50,900 --> 00:02:54,680 It's going to create a new hash using the hash class in Ruby. 48 00:02:54,680 --> 00:02:55,920 And let's just build it up this way. 49 00:02:55,920 --> 00:03:03,160 Let's say we've got snack carbs and we want to assign the key candy the value of 15, like 50 00:03:03,160 --> 00:03:04,160 that. 51 00:03:04,160 --> 00:03:07,240 Now when we index snack carbs candy, we get back 15. 52 00:03:07,240 --> 00:03:09,320 So let's put a couple more in there. 53 00:03:09,320 --> 00:03:13,640 Let's say we want to put in pretzel like that and we're going to assign it 0, 10. 54 00:03:13,640 --> 00:03:18,160 I think I did 100 last time, so we'll give pretzel 100. 55 00:03:18,160 --> 00:03:19,160 Index that. 56 00:03:19,160 --> 00:03:20,160 Sure enough, we have 100. 57 00:03:20,160 --> 00:03:21,160 How about soda? 58 00:03:21,160 --> 00:03:27,440 Well, we'll put in, fill up the hash that way and we get back five. 59 00:03:27,440 --> 00:03:32,680 So now if we look at the hash, snack carbs, there's our hash statement there. 60 00:03:32,680 --> 00:03:35,900 And hashes are objects in Ruby, so lots of methods on hashes. 61 00:03:35,900 --> 00:03:39,600 Some of the more convenient ones would be something like if we wanted to get all the 62 00:03:39,600 --> 00:03:43,200 keys, it'll return an array of the keys, candy, pretzel, and soda. 63 00:03:43,200 --> 00:03:45,720 Or maybe we wanted to just get all the values. 64 00:03:45,720 --> 00:03:48,080 It returns an array of values. 65 00:03:48,080 --> 00:03:51,000 Now like arrays, you can iterate through a hash. 66 00:03:51,000 --> 00:03:53,360 Let's take a look at how that works. 67 00:03:53,360 --> 00:03:55,820 Here's our hash of snacks and carbs. 68 00:03:55,820 --> 00:04:00,240 Like arrays, hashes have an each method and that takes a block. 69 00:04:00,240 --> 00:04:05,320 If we declare the block to take two parameters, the first will be the key and the second will 70 00:04:05,320 --> 00:04:07,000 be its value. 71 00:04:07,000 --> 00:04:11,680 So when we iterate through this hash, the first time through candy is assigned to the 72 00:04:11,680 --> 00:04:19,760 name variable and 15 is assigned to the carbs variable and we get candy has 15 carbs. 73 00:04:19,760 --> 00:04:25,240 Then we iterate through the rest of the hash and we get pretzel has 10 carbs and soda has 74 00:04:25,240 --> 00:04:26,560 five carbs. 75 00:04:26,560 --> 00:04:28,760 So let's show how this works in IRB, Mike. 76 00:04:28,760 --> 00:04:32,800 Sure, we'll just use our snack carbs hash. 77 00:04:32,800 --> 00:04:35,680 We'll call the each method that takes a block, single line block. 78 00:04:35,680 --> 00:04:41,240 I'll just use braces there and it passes as two block parameters, the key and the value. 79 00:04:41,240 --> 00:04:52,280 And then inside there, I'll just do a put s and I'll say key has value carbs. 80 00:04:52,280 --> 00:04:55,920 Candy has 15 carbs, pretzel has 100, and soda has five. 81 00:04:55,920 --> 00:04:59,720 So we've seen how to set keys and values in a hash, but we actually want to use this hash 82 00:04:59,720 --> 00:05:03,440 to accumulate the total carbs for any particular snack. 83 00:05:03,440 --> 00:05:05,480 So let's look at how to do that. 84 00:05:05,480 --> 00:05:09,960 So let's just start over here with our snack carbs and we're just going to use a empty 85 00:05:09,960 --> 00:05:11,600 hash for now. 86 00:05:11,600 --> 00:05:17,800 And let's say that when a movie runs, it caused a candy to be consumed. 87 00:05:17,800 --> 00:05:20,240 So we want to assign the value 15 to candy. 88 00:05:20,240 --> 00:05:22,600 So candy, we've got 15 carbs. 89 00:05:22,600 --> 00:05:27,960 Now let's say we run the next viewing and another candy snack got consumed. 90 00:05:27,960 --> 00:05:30,280 So we want to add 15 to the existing 15. 91 00:05:30,280 --> 00:05:32,560 Well, we can do that using plus equals. 92 00:05:32,560 --> 00:05:36,599 It's going to take the value that's in the candy or that's associated with the candy 93 00:05:36,599 --> 00:05:38,780 key, add 15 to it. 94 00:05:38,780 --> 00:05:43,799 So if we look at our snack carbs hash now, it's got candy 30. 95 00:05:43,799 --> 00:05:46,560 It updated the value for that key. 96 00:05:46,560 --> 00:05:51,039 So what happens if we index into the hash with a key that doesn't have a value? 97 00:05:51,039 --> 00:05:52,640 Let's say soda in this case. 98 00:05:52,640 --> 00:05:54,020 Well, we get back nil. 99 00:05:54,020 --> 00:05:56,200 There's no value associated with that. 100 00:05:56,200 --> 00:06:00,120 And if we were to try to accumulate soda, so we haven't consumed a soda, but if we were 101 00:06:00,120 --> 00:06:06,320 to use the syntax plus equals to add five to that existing key, well, we have ourselves 102 00:06:06,320 --> 00:06:11,920 a problem because Ruby's trying to add nil, which is the value that's currently associated 103 00:06:11,920 --> 00:06:14,240 with that key, and five together. 104 00:06:14,240 --> 00:06:16,020 And that's just not going to work. 105 00:06:16,020 --> 00:06:21,300 So we kind of need a way to set up our hash to instead of having a default value of nil 106 00:06:21,300 --> 00:06:26,460 for keys that don't have a value, instead have it have a default value of zero. 107 00:06:26,460 --> 00:06:31,800 And the way we do that is when we create the hash, this is snack carbs, we can create it 108 00:06:31,800 --> 00:06:36,960 using the hash class, pass in new or call the new method, and then pass in what the 109 00:06:36,960 --> 00:06:38,820 default value should be. 110 00:06:38,820 --> 00:06:42,460 So we're going to say the default value should be zero. 111 00:06:42,460 --> 00:06:48,640 That way, if we call snack carbs and we ask for soda, instead of getting back nil, we 112 00:06:48,640 --> 00:06:50,500 get back zero. 113 00:06:50,500 --> 00:06:53,260 And this is good because then we can go ahead and add things. 114 00:06:53,260 --> 00:06:56,340 They may not already exist there, and we won't get this error. 115 00:06:56,340 --> 00:07:00,840 So if we were to say add plus equals five, well, we just get five. 116 00:07:00,840 --> 00:07:04,720 And if we were to consume another soda, well, we're going to end up with 10. 117 00:07:04,720 --> 00:07:09,640 We're going to need to do something like that inside of our movie application so that snacks 118 00:07:09,640 --> 00:07:13,700 always have a carb total of zero starting out. 119 00:07:13,700 --> 00:07:15,080 So here's what we have now. 120 00:07:15,080 --> 00:07:20,100 We have our snack bar, and when we play a movie, we call the random method, and we get 121 00:07:20,100 --> 00:07:22,880 a snack object, and we print it out. 122 00:07:22,880 --> 00:07:25,940 But we aren't storing it with the movie. 123 00:07:25,940 --> 00:07:31,000 Now what we want to do is store the snacks that were consumed during each movie. 124 00:07:31,000 --> 00:07:35,400 Inside the movie class, we won't store the snack object itself. 125 00:07:35,400 --> 00:07:40,140 Instead we'll store the snack name and its accumulated carbs in a hash. 126 00:07:40,140 --> 00:07:46,180 In the hash, the key will become the name of the snack, and the value will be the accumulated 127 00:07:46,180 --> 00:07:47,320 carbs. 128 00:07:47,320 --> 00:07:54,980 That way our hash fills up like this, popcorn for 20, candy for 15, nachos for 40, but then 129 00:07:54,980 --> 00:07:59,920 when we add the second candy that comes back around, candy will go to 30. 130 00:07:59,920 --> 00:08:04,700 Bear in mind, however, that we want to accumulate snacks for each movie, so we need a hash for 131 00:08:04,700 --> 00:08:05,900 each movie. 132 00:08:05,900 --> 00:08:08,700 We'll use a snack carbs instance variable. 133 00:08:08,700 --> 00:08:13,420 That way, as we iterate through the movies, each movie will have a hash in which to accumulate 134 00:08:13,420 --> 00:08:14,740 its snacks. 135 00:08:14,740 --> 00:08:16,780 We'll need an instance variable for that. 136 00:08:16,780 --> 00:08:20,739 Remember that values of instance variables are unique for each object. 137 00:08:20,739 --> 00:08:22,340 So let's go do that. 138 00:08:22,340 --> 00:08:26,219 Returning to our playlist class here, every time we play a movie, we're grabbing this 139 00:08:26,219 --> 00:08:29,619 random snack, and now we want to hand that off to the movie somehow. 140 00:08:29,619 --> 00:08:33,380 So I'm just going to take this line of code right here. 141 00:08:33,380 --> 00:08:34,380 We've got a movie object. 142 00:08:34,380 --> 00:08:40,720 What we want to be able to do is call something like movie8snack, and then pass in that snack. 143 00:08:40,720 --> 00:08:43,980 That way the movie can take credit for the snack that was consumed while it was being 144 00:08:43,980 --> 00:08:44,980 viewed. 145 00:08:44,980 --> 00:08:48,660 We're going to take this out of here too, we don't need that in there anymore. 146 00:08:48,660 --> 00:08:55,060 So then over in my movie class, I'm going to need to store these snacks in a hash, as 147 00:08:55,060 --> 00:08:56,060 Nicole said. 148 00:08:56,060 --> 00:09:02,540 So when I set up my movie class, I'm going to go ahead and initialize a snack carbs hash. 149 00:09:02,540 --> 00:09:04,400 And remember, I want to use this syntax. 150 00:09:04,400 --> 00:09:07,060 I want it to default to values of zero. 151 00:09:07,060 --> 00:09:11,260 So if we ask it for a snack that's not there, it's just going to say it has zero carbs. 152 00:09:11,260 --> 00:09:15,060 Now we need to define this 8snack method that we are calling over in Playlist. 153 00:09:15,060 --> 00:09:16,060 So I'm just going to do it right up here. 154 00:09:16,060 --> 00:09:22,340 It's going to be called 8snack, and we're going to pass in a snack object here. 155 00:09:22,340 --> 00:09:24,100 So what do we want to do inside of here? 156 00:09:24,100 --> 00:09:29,040 Well, we want to accumulate things inside of our snack carbs hash, snack carbs. 157 00:09:29,040 --> 00:09:32,460 So the key is going to be the name of the snack. 158 00:09:32,460 --> 00:09:36,319 Remember, the name of the snack is an attribute on the snack object. 159 00:09:36,319 --> 00:09:39,220 It also happens to be a symbol, which works out nicely. 160 00:09:39,220 --> 00:09:45,140 The value of that key is going to be the carbs of the snack. 161 00:09:45,140 --> 00:09:49,220 Now if we did the assignment this way, then it's just always going to be the most recent 162 00:09:49,220 --> 00:09:50,880 snack that got consumed. 163 00:09:50,880 --> 00:09:53,860 We want to accumulate, so we want to do plus equals. 164 00:09:53,860 --> 00:09:58,980 That way, as we accumulate more snacks, then that carb total will continue to go up. 165 00:09:58,980 --> 00:10:03,660 And then at the end of this, I'm just going to print out the statement that we had before. 166 00:10:03,660 --> 00:10:10,540 I can just print out our title led to snack carbs being consumed, just like we had before 167 00:10:10,540 --> 00:10:11,540 there. 168 00:10:11,540 --> 00:10:14,260 And then I'm going to put one more thing at the bottom, just so that we can see it. 169 00:10:14,260 --> 00:10:20,140 I'm going to print out the movie title snacks. 170 00:10:20,140 --> 00:10:24,020 And then I just want to print out the whole hash here, and I'm going to do that by doing 171 00:10:24,020 --> 00:10:26,420 snack carbs. 172 00:10:26,420 --> 00:10:28,500 And it'll just print out the hash for us. 173 00:10:28,500 --> 00:10:30,120 So there's our method for eating the snack. 174 00:10:30,120 --> 00:10:36,000 We can go back over to our Flix file now and go ahead and run it. 175 00:10:36,000 --> 00:10:38,280 So let's just look at viewing one. 176 00:10:38,280 --> 00:10:41,100 It says Goonies led to 15 snack carbs being consumed. 177 00:10:41,100 --> 00:10:45,540 And then we're printing out Goonies snacks here, and we see that it just got one thing 178 00:10:45,540 --> 00:10:47,360 inside of its array. 179 00:10:47,360 --> 00:10:51,860 Now down here in the second viewing, make this a little bit bigger, the second viewing, 180 00:10:51,860 --> 00:10:57,060 Goonies now has two snacks, candy 15, pretzel 10. 181 00:10:57,060 --> 00:11:01,979 So here we can see in viewing three, Goonies has one candy, but notice that it's got a 182 00:11:01,979 --> 00:11:04,859 pretzel total carb count of 20. 183 00:11:04,859 --> 00:11:10,119 Pretzels only worth 10 each, so it's consumed two pretzels, or it led to two pretzels and 184 00:11:10,119 --> 00:11:11,760 one candy being consumed. 185 00:11:11,760 --> 00:11:15,099 So our accumulation in the hash is working as we expect. 186 00:11:15,099 --> 00:11:19,719 Now what if we wanted to total up all the carbs consumed by the movie, like two pretzels 187 00:11:19,719 --> 00:11:22,500 plus three nachos plus four sodas? 188 00:11:22,500 --> 00:11:23,500 How many carbs is that? 189 00:11:23,500 --> 00:11:27,620 Well, the total carbs that the movie consumed after all the viewings. 190 00:11:27,620 --> 00:11:28,620 Right. 191 00:11:28,620 --> 00:11:32,400 Well, you know, we can make pretty quick work of that using that reduce method we saw earlier. 192 00:11:32,400 --> 00:11:33,540 So let's revisit that again. 193 00:11:33,540 --> 00:11:34,820 Let's do that. 194 00:11:34,820 --> 00:11:40,560 So let's say we have an array of carbs like this, 10, 20, 30, right? 195 00:11:40,560 --> 00:11:42,140 And we want to sum all those up. 196 00:11:42,140 --> 00:11:44,060 Well, we could use reduce. 197 00:11:44,060 --> 00:11:45,960 One form of reduce takes a block. 198 00:11:45,960 --> 00:11:50,660 The first block parameter is the accumulator, and then the element being passed in, and 199 00:11:50,660 --> 00:11:54,819 we could say sum plus in, and we get back 60. 200 00:11:54,819 --> 00:12:00,540 Another way to do that that we saw is we can pass a symbol and then an operation, let's 201 00:12:00,540 --> 00:12:04,699 say the plus operation, and it will tally up all the carbs that way. 202 00:12:04,699 --> 00:12:06,260 But what if the array is empty? 203 00:12:06,260 --> 00:12:10,620 Well, another form of this is to pass in the initial value. 204 00:12:10,620 --> 00:12:14,699 So if the carbs array is empty, we'll just get back zero. 205 00:12:14,699 --> 00:12:16,579 Otherwise it'll tally all those up. 206 00:12:16,580 --> 00:12:20,580 So we probably want to use this form just in case the movie hasn't consumed any carbs 207 00:12:20,580 --> 00:12:22,060 yet. 208 00:12:22,060 --> 00:12:26,940 So let's go back over to our movie class, and we can go ahead and define a method that 209 00:12:26,940 --> 00:12:28,220 will tally up all the carbs. 210 00:12:28,220 --> 00:12:31,420 So let's just call it carbs consumed. 211 00:12:31,420 --> 00:12:35,140 And inside of that method, we're just going to take our snack carbs hash. 212 00:12:35,140 --> 00:12:38,940 Remember we can get all the values of a hash by calling the values method. 213 00:12:38,940 --> 00:12:41,680 This is going to be all the carb numbers, right? 214 00:12:41,680 --> 00:12:45,360 And then we can call reduce on it because it's just an array. 215 00:12:45,360 --> 00:12:50,140 We'll have an initial value of zero, and then we'll have it add all of those up. 216 00:12:50,140 --> 00:12:52,320 So that's the method to total up all the carbs. 217 00:12:52,320 --> 00:12:56,460 If we go back over to the playlist now to use that method, we'll do it down in print 218 00:12:56,460 --> 00:12:57,820 stats. 219 00:12:57,820 --> 00:13:01,820 Let's say we do it, oh, maybe at the top here before we print out the hits and the flops. 220 00:13:01,820 --> 00:13:03,620 We'll just take our movies array. 221 00:13:03,620 --> 00:13:04,940 We'll sort them because we can sort them. 222 00:13:04,940 --> 00:13:06,700 We've got a sorting method now. 223 00:13:06,700 --> 00:13:09,060 That's going to pass us a movie object. 224 00:13:09,060 --> 00:13:18,739 And then we'll print out something like, let's see, movie.title, so that's a form snack totals 225 00:13:18,739 --> 00:13:20,219 like that. 226 00:13:20,219 --> 00:13:23,819 And then we'll print out, well, let's see, movie. 227 00:13:23,819 --> 00:13:29,660 Now we can call our method carbs consumed, grand total carbs. 228 00:13:29,660 --> 00:13:32,140 Go to our Flix file. 229 00:13:32,140 --> 00:13:35,939 If we run it, we look down at the bottom here. 230 00:13:35,939 --> 00:13:37,380 Here's Kermit stats. 231 00:13:37,380 --> 00:13:43,860 So Ghostbusters, 45 grand total carbs, Goonies 30, and Goldfinger 40. 232 00:13:43,860 --> 00:13:48,660 Now later we'll break this out on a per snack basis, but for now, let's tally up all the 233 00:13:48,660 --> 00:13:54,060 carbs like all of Goonies carbs plus Ghostbusters plus Goldfinger's carbs. 234 00:13:54,060 --> 00:13:56,780 So we're going to need a new method on the playlist to do that. 235 00:13:56,780 --> 00:13:59,500 It's all the carbs for all the movies. 236 00:13:59,500 --> 00:14:01,420 You're really liking this reduced method thing, aren't you? 237 00:14:01,420 --> 00:14:02,460 Oh yeah, very much. 238 00:14:02,460 --> 00:14:03,860 Let's try that. 239 00:14:03,860 --> 00:14:08,760 So go back over to the playlist here, and what we want to do is maybe up here we want 240 00:14:08,760 --> 00:14:13,860 to print something like, let's just set up a little goal for ourselves here. 241 00:14:13,860 --> 00:14:18,960 We want to be able to call a method like total carbs consumed. 242 00:14:18,960 --> 00:14:23,860 This is for the entire playlist, and then we'll print total carbs consumed just like 243 00:14:23,860 --> 00:14:24,860 that. 244 00:14:24,860 --> 00:14:26,220 So how do we implement that? 245 00:14:26,220 --> 00:14:31,260 Well, we'll define our method total carbs consumed like that. 246 00:14:31,260 --> 00:14:33,400 Give ourselves a little space. 247 00:14:33,400 --> 00:14:38,220 So what we want to do is we want to loop through all the movies, and we want to accumulate 248 00:14:38,220 --> 00:14:40,660 all of their carb totals. 249 00:14:40,660 --> 00:14:42,079 So we're going to take our movies. 250 00:14:42,079 --> 00:14:44,300 We can say reduce like that. 251 00:14:44,300 --> 00:14:47,480 Now I'm going to pass a parameter to reduce zero. 252 00:14:47,480 --> 00:14:51,100 This is going to be the initial value that it's going to use in case we don't have anything 253 00:14:51,100 --> 00:14:52,860 in that movies array. 254 00:14:52,860 --> 00:14:54,300 And we're going to give it a block. 255 00:14:54,300 --> 00:14:59,740 Remember the block to reduce takes an accumulator sum, and then it's going to give us each movie. 256 00:14:59,740 --> 00:15:03,900 So it's going to iterate through, but it's also going to reduce everything down to a 257 00:15:03,900 --> 00:15:04,900 number. 258 00:15:04,900 --> 00:15:08,620 So inside of there, we're just going to add to the accumulator sum. 259 00:15:08,620 --> 00:15:09,620 We're going to add movie. 260 00:15:09,620 --> 00:15:15,860 And remember, we just wrote this method on movie called carbs consumed. 261 00:15:15,860 --> 00:15:23,620 So reduce the movies array down to the sum of all the carbs consumed across all the movies. 262 00:15:23,620 --> 00:15:24,620 Right. 263 00:15:24,620 --> 00:15:25,620 Right. 264 00:15:25,620 --> 00:15:26,620 Go back to Flix. 265 00:15:26,620 --> 00:15:33,300 We run that down in our stats, Kermit stats, 200 total carbs consumed, which should be 266 00:15:33,300 --> 00:15:37,420 the total of 75, 65, and 60. 267 00:15:37,420 --> 00:15:38,720 All right. 268 00:15:38,720 --> 00:15:39,720 You guessed it. 269 00:15:39,720 --> 00:15:40,820 Now it's your turn. 270 00:15:40,820 --> 00:15:46,020 In the same way that we stored snacks for each movie, in the exercise, you're going 271 00:15:46,020 --> 00:15:49,720 to store found treasures for each player. 272 00:15:49,720 --> 00:15:53,700 And then you're going to add up the total treasure points and actually change your player's 273 00:15:53,700 --> 00:15:54,700 score. 274 00:15:54,700 --> 00:15:58,340 And coming up, we're going to write our own iterator method so that we can print a player's 275 00:15:58,340 --> 00:16:01,100 points on a per treasure basis. 276 00:16:01,100 --> 00:16:25,300 Have fun. 24852

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.