All language subtitles for 13. The reduce Method

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)
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 Download
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 1 00:00:01,350 --> 00:00:03,340 In this video, we're gonna talk about 2 2 00:00:03,340 --> 00:00:06,230 the third data transformations method, 3 3 00:00:06,230 --> 00:00:08,363 which is the reduce method. 4 4 00:00:09,810 --> 00:00:11,480 And as you will remember, 5 5 00:00:11,480 --> 00:00:15,680 we use the reduce method to essentially boil down 6 6 00:00:15,680 --> 00:00:20,300 all the elements in an array to one single value. 7 7 00:00:20,300 --> 00:00:22,400 And we talked about the example of 8 8 00:00:22,400 --> 00:00:25,976 adding up all the numbers in one array, right? 9 9 00:00:25,976 --> 00:00:29,443 And so let's try that now with all movements array. 10 10 00:00:31,130 --> 00:00:35,493 And remember that array looks like this. Okay. 11 11 00:00:36,354 --> 00:00:39,190 And then by adding up all these numbers, 12 12 00:00:39,190 --> 00:00:41,682 so both the deposits and the withdrawals, 13 13 00:00:41,682 --> 00:00:46,682 we end up with the global balance of the account, basically. 14 14 00:00:46,850 --> 00:00:51,320 So let's start using the reduce method here. 15 15 00:00:51,320 --> 00:00:52,630 And so, as I just said, 16 16 00:00:52,630 --> 00:00:57,360 we're gonna call it on the movements array. 17 17 00:00:57,360 --> 00:01:00,590 And the results of the reduce method will be, 18 18 00:01:00,590 --> 00:01:04,223 as I just said, the global balance of the account. 19 19 00:01:06,260 --> 00:01:08,850 So let's call the result, balance. 20 20 00:01:08,850 --> 00:01:10,540 And remember that in this case, 21 21 00:01:10,540 --> 00:01:13,309 this balance will be simply one value 22 22 00:01:13,309 --> 00:01:15,383 and not an entire array. 23 23 00:01:16,928 --> 00:01:21,928 Okay. So the reduce function also gets a callback function, 24 24 00:01:22,610 --> 00:01:26,050 but this one is a little bit different from the other ones, 25 25 00:01:26,050 --> 00:01:28,825 like the one in map or for each. 26 26 00:01:28,825 --> 00:01:30,593 So in these other callbacks, 27 27 00:01:30,593 --> 00:01:32,860 the first parameter is always 28 28 00:01:32,860 --> 00:01:36,240 the current element of the array. 29 29 00:01:36,240 --> 00:01:37,500 Let's call it current. 30 30 00:01:37,500 --> 00:01:39,490 The second one is the current index 31 31 00:01:39,490 --> 00:01:42,630 and the third one is the entire array. 32 32 00:01:42,630 --> 00:01:45,915 But here in the callback function of the reduce method, 33 33 00:01:45,915 --> 00:01:48,410 the first parameter is actually 34 34 00:01:48,410 --> 00:01:50,950 something called the accumulator. 35 35 00:01:50,950 --> 00:01:55,030 So let's call it acc like this 36 36 00:01:55,030 --> 00:01:57,890 as an abbreviation of accumulator. 37 37 00:01:57,890 --> 00:02:02,030 And this accumulator is essentially like a snowball 38 38 00:02:02,030 --> 00:02:04,193 that keeps accumulating the value 39 39 00:02:04,193 --> 00:02:06,570 that we ultimately want to return. 40 40 00:02:06,570 --> 00:02:09,650 So in the case of adding all the elements 41 41 00:02:09,650 --> 00:02:12,445 or all the numbers of an array together, 42 42 00:02:12,445 --> 00:02:14,780 that will be the sum. 43 43 00:02:14,780 --> 00:02:16,580 So let me write that here. 44 44 00:02:16,580 --> 00:02:17,413 Oops. 45 45 00:02:18,410 --> 00:02:21,370 So let me write accumulator 46 46 00:02:22,260 --> 00:02:24,303 it's like a snowball. 47 47 00:02:25,779 --> 00:02:26,923 All right. 48 48 00:02:29,200 --> 00:02:33,520 But anyway, let's not write the actual function block here. 49 49 00:02:33,520 --> 00:02:36,710 So as always, this callback function here 50 50 00:02:36,710 --> 00:02:38,940 will be called in each iteration 51 51 00:02:38,940 --> 00:02:41,460 of a looping over the array. 52 52 00:02:41,460 --> 00:02:44,100 So reduce also loops over the array 53 53 00:02:44,100 --> 00:02:47,480 and calls this callback in each iteration, 54 54 00:02:47,480 --> 00:02:50,050 but now what will we actually do 55 55 00:02:50,050 --> 00:02:51,998 in each of these iterations? 56 56 00:02:51,998 --> 00:02:55,333 Well, since the accumulator is the value 57 57 00:02:55,333 --> 00:02:58,890 that we will keep adding to what we're gonna do here 58 58 00:02:58,890 --> 00:03:02,063 is to add the current value to the accumulator. 59 59 00:03:03,360 --> 00:03:08,360 So the accumulator plus the current value. Okay. 60 60 00:03:09,510 --> 00:03:12,060 And this works because in each call of 61 61 00:03:12,060 --> 00:03:13,310 the callback function, 62 62 00:03:13,310 --> 00:03:15,840 the accumulator will be the current sum 63 63 00:03:15,840 --> 00:03:18,190 of all the previous values. 64 64 00:03:18,190 --> 00:03:21,430 And so we will really keep adding to this accumulator 65 65 00:03:21,430 --> 00:03:23,673 in each iteration of the loop. 66 66 00:03:25,010 --> 00:03:28,660 Finally, we also need to return this value here 67 67 00:03:28,660 --> 00:03:30,000 from the callback. 68 68 00:03:30,000 --> 00:03:32,240 And so this is how the new accumulator 69 69 00:03:32,240 --> 00:03:35,992 can then be used in the next iteration of the loop. 70 70 00:03:35,992 --> 00:03:38,770 So basically in each loop iteration, 71 71 00:03:38,770 --> 00:03:41,325 we return the updated accumulator 72 72 00:03:41,325 --> 00:03:44,465 so the current one, plus the new current value. 73 73 00:03:44,465 --> 00:03:48,010 And so like this, we can then keep adding to it 74 74 00:03:48,010 --> 00:03:49,863 in the next iteration. 75 75 00:03:49,863 --> 00:03:50,763 Okay? 76 76 00:03:51,610 --> 00:03:55,810 So this callback function is the first argument 77 77 00:03:55,810 --> 00:03:57,600 of the reduce method, 78 78 00:03:57,600 --> 00:04:01,010 but the reduce method actually has a another, 79 79 00:04:01,010 --> 00:04:03,020 so a second parameter, 80 80 00:04:03,020 --> 00:04:06,640 and that is the initial value of the accumulator. 81 81 00:04:06,640 --> 00:04:08,794 So the value that we specify here, 82 82 00:04:08,794 --> 00:04:12,763 which in this case is gonna be zero is the initial value 83 83 00:04:12,763 --> 00:04:16,300 of the accumulator in the first loop iteration. 84 84 00:04:16,300 --> 00:04:20,047 And so in this example, we want to start counting 85 85 00:04:20,047 --> 00:04:23,420 or we want to start adding at zero. 86 86 00:04:23,420 --> 00:04:26,893 And so therefore we simply specify zero here. 87 87 00:04:30,290 --> 00:04:32,626 Okay, and with this we should be able 88 88 00:04:32,626 --> 00:04:35,441 to already take a look at our balance. 89 89 00:04:35,441 --> 00:04:39,450 And yeah, so we have one single number. 90 90 00:04:39,450 --> 00:04:44,450 So indeed everything was boiled down to this one number, 91 91 00:04:45,250 --> 00:04:48,580 which we can suppose is all of these values here 92 92 00:04:48,580 --> 00:04:51,339 added together, all right. 93 93 00:04:51,339 --> 00:04:54,179 And to make this even more clear to see, 94 94 00:04:54,179 --> 00:04:58,920 that's actually always log the accumulator to the console. 95 95 00:04:58,920 --> 00:05:02,877 So let's say iteration number I 96 96 00:05:06,430 --> 00:05:08,233 and enter the accumulator in there. 97 97 00:05:12,180 --> 00:05:15,380 Okay, so we see that in iteration zero, 98 98 00:05:15,380 --> 00:05:17,970 the accumulator is zero. 99 99 00:05:17,970 --> 00:05:20,710 And so this is the value that we specified here 100 100 00:05:20,710 --> 00:05:23,800 as the starter so as the initial value. 101 101 00:05:23,800 --> 00:05:25,170 Then in the second iteration, 102 102 00:05:25,170 --> 00:05:28,090 the accumulator is already at 200 103 103 00:05:28,090 --> 00:05:30,589 and that's because that is the initial value 104 104 00:05:30,589 --> 00:05:33,870 plus the current value in the previous iteration. 105 105 00:05:33,870 --> 00:05:35,283 So that was 200 there. 106 106 00:05:37,091 --> 00:05:41,220 And so this zero plus the 200 creates the new accumulator, 107 107 00:05:41,220 --> 00:05:44,460 which is done in the next iteration, 200. 108 108 00:05:44,460 --> 00:05:48,000 Then in this iteration, we add the current value again, 109 109 00:05:48,000 --> 00:05:49,840 which is 450. 110 110 00:05:49,840 --> 00:05:52,190 So that result is 650. 111 111 00:05:52,190 --> 00:05:53,888 And therefore in the next iteration, 112 112 00:05:53,888 --> 00:05:56,263 that's the value of the accumulator. 113 113 00:05:57,250 --> 00:05:59,532 Okay. And so here you can really see 114 114 00:05:59,532 --> 00:06:02,970 like the snowball effect of all of these values, 115 115 00:06:02,970 --> 00:06:05,530 adding up to one final value. 116 116 00:06:05,530 --> 00:06:09,279 And in the end, that value is essentially the accumulator. 117 117 00:06:09,279 --> 00:06:11,770 Now here we see the value of going down 118 118 00:06:11,770 --> 00:06:13,200 because in this iteration, 119 119 00:06:13,200 --> 00:06:15,340 we are adding minus 400 120 120 00:06:15,340 --> 00:06:19,533 and so 250 is 650 minus 400. 121 121 00:06:21,330 --> 00:06:25,283 Okay, so in the last iteration the value is 2,540, 122 122 00:06:27,520 --> 00:06:31,080 and then adding 1,300 will eventually result 123 123 00:06:31,080 --> 00:06:32,943 in this value here. 124 124 00:06:33,970 --> 00:06:38,420 Great. So this is really how the reduce method works. 125 125 00:06:38,420 --> 00:06:40,260 And I think this a log here 126 126 00:06:40,260 --> 00:06:42,333 is a great visualization of that. 127 127 00:06:43,230 --> 00:06:45,833 Let's give it all the space we need. 128 128 00:06:45,833 --> 00:06:48,986 And let me just change this one here to 100, 129 129 00:06:48,986 --> 00:06:52,870 so that you see the effect of changing this. 130 130 00:06:52,870 --> 00:06:56,220 And so now we can expect this accumulator 131 131 00:06:56,220 --> 00:06:59,320 in the beginning being 100. 132 132 00:06:59,320 --> 00:07:02,660 And so we already start with a small snowball here, 133 133 00:07:02,660 --> 00:07:04,981 which then we keep adding to, 134 134 00:07:04,981 --> 00:07:09,347 and therefore our final balance is now 100 euros 135 135 00:07:09,347 --> 00:07:12,793 or whatever it is larger. 136 136 00:07:13,950 --> 00:07:18,300 Okay. And yeah, 137 137 00:07:18,300 --> 00:07:21,270 let's one more time do the same thing manually, 138 138 00:07:21,270 --> 00:07:22,903 basically with a four loop. 139 139 00:07:26,140 --> 00:07:29,480 So of movements. 140 140 00:07:29,480 --> 00:07:33,380 And so we need one more time, an external variable here, 141 141 00:07:33,380 --> 00:07:35,907 which is gonna be the sum. 142 142 00:07:35,907 --> 00:07:39,290 Well, let's call it balance two. 143 143 00:07:39,290 --> 00:07:41,770 So balance two starts at zero. 144 144 00:07:41,770 --> 00:07:42,920 And so essentially this is, 145 145 00:07:42,920 --> 00:07:46,630 or initial accumulator value just like this zero 146 146 00:07:47,800 --> 00:07:52,800 and then sum plus equal the current movement. 147 147 00:07:54,750 --> 00:07:58,623 And if we lock balance two, we will get, okay. 148 148 00:08:00,330 --> 00:08:03,903 Now of course this has to be balance two, 149 149 00:08:03,903 --> 00:08:07,320 but now we get indeed the same result. 150 150 00:08:07,320 --> 00:08:10,800 And so here you can see this common pattern 151 151 00:08:10,800 --> 00:08:13,090 that we always need an external variable 152 152 00:08:13,090 --> 00:08:16,160 whenever we want to use a for loop. 153 153 00:08:16,160 --> 00:08:18,840 And that's fine if you only need one loop, 154 154 00:08:18,840 --> 00:08:21,870 but it starts to become really cumbersome 155 155 00:08:21,870 --> 00:08:24,710 and unpractical when we use many loops 156 156 00:08:24,710 --> 00:08:26,588 for doing many operations. 157 157 00:08:26,588 --> 00:08:29,350 So these methods that we've been studying, 158 158 00:08:29,350 --> 00:08:32,340 they completely avoid this extra variable 159 159 00:08:32,340 --> 00:08:34,570 and they simply return the variable 160 160 00:08:34,570 --> 00:08:37,238 or the value actually right away. 161 161 00:08:37,238 --> 00:08:38,153 All right. 162 162 00:08:39,278 --> 00:08:44,278 And of course we can write here in an even simpler way. 163 163 00:08:45,035 --> 00:08:47,127 So using an arrow function 164 164 00:08:47,127 --> 00:08:49,079 and I will still keep this here 165 165 00:08:49,079 --> 00:08:52,283 so that we can see the return that's happening. 166 166 00:08:53,670 --> 00:08:56,291 So first we can get rid of this. 167 167 00:08:56,291 --> 00:08:58,720 Then we get rid of this. 168 168 00:08:58,720 --> 00:09:00,633 We also don't need any of this, 169 169 00:09:01,490 --> 00:09:06,100 but here we always use or need the accumulator 170 170 00:09:06,100 --> 00:09:07,303 and the current value. 171 171 00:09:08,290 --> 00:09:09,870 So here we need parenthesis 172 172 00:09:10,800 --> 00:09:12,556 then the arrow. 173 173 00:09:12,556 --> 00:09:17,556 And then as always, we can get rid of all of this. 174 174 00:09:18,830 --> 00:09:23,710 And so now still, or balanced is the same. Awesome. 175 175 00:09:23,710 --> 00:09:26,700 So understanding how the reduce method works 176 176 00:09:26,700 --> 00:09:28,940 is something really important, 177 177 00:09:28,940 --> 00:09:32,220 but it's also way more confusing than the other ones, 178 178 00:09:32,220 --> 00:09:34,350 but I'm sure you're well underway 179 179 00:09:34,350 --> 00:09:36,366 of really understanding it. 180 180 00:09:36,366 --> 00:09:38,740 And now that we know how it works, 181 181 00:09:38,740 --> 00:09:42,600 we can actually now also calculate the balance 182 182 00:09:42,600 --> 00:09:46,460 of these movements here and then print that balance 183 183 00:09:46,460 --> 00:09:49,663 right here to our application and user interface. 184 184 00:09:50,560 --> 00:09:53,243 And so let's go ahead and do that here. 185 185 00:09:54,490 --> 00:09:57,155 So finally back to our application here. 186 186 00:09:57,155 --> 00:10:00,103 And so let's do that down here. 187 187 00:10:01,810 --> 00:10:06,363 So calc and print, balance. 188 188 00:10:09,880 --> 00:10:12,380 And so this function here will once again, 189 189 00:10:12,380 --> 00:10:15,273 receive the movements as an input. 190 190 00:10:16,110 --> 00:10:18,273 So any array of movements will work. 191 191 00:10:19,363 --> 00:10:24,363 And so now let's calculate the balance based on this array. 192 192 00:10:26,380 --> 00:10:29,943 And essentially this is exactly what we wrote to before. 193 193 00:10:31,220 --> 00:10:35,220 So that's movement dot reduce, 194 194 00:10:35,220 --> 00:10:38,360 but it doesn't hurt to write it here again. 195 195 00:10:38,360 --> 00:10:42,680 So remember the first parameter is now the accumulator 196 196 00:10:42,680 --> 00:10:45,810 and only the second one is the current value. 197 197 00:10:45,810 --> 00:10:48,163 So let's call it the movement now. 198 198 00:10:49,915 --> 00:10:54,372 And so here simply we return the accumulator 199 199 00:10:54,372 --> 00:10:58,793 plus the current movement and we start at zero. 200 200 00:10:59,750 --> 00:11:03,330 Great. And now all we need to do is to display it 201 201 00:11:03,330 --> 00:11:05,210 here in this element. 202 202 00:11:05,210 --> 00:11:10,210 So let's check this out and it is called the balance value. 203 203 00:11:10,500 --> 00:11:13,593 And just like before I already have it selected here, 204 204 00:11:14,840 --> 00:11:19,300 and this is a label, so balance value is here. 205 205 00:11:19,300 --> 00:11:21,047 So I called it label balance 206 206 00:11:21,047 --> 00:11:24,110 and so label is basically all the things 207 207 00:11:24,110 --> 00:11:27,983 where we simply want to put some text, all right? 208 208 00:11:33,785 --> 00:11:36,607 So let's say label balance dot text content. 209 209 00:11:37,620 --> 00:11:42,060 And this one we already know equals the balance 210 210 00:11:43,000 --> 00:11:47,470 and let's actually do a template string here, 211 211 00:11:47,470 --> 00:11:51,926 or a template literal and also display the Euro sign. 212 212 00:11:51,926 --> 00:11:53,710 Well, I don't know where it is, 213 213 00:11:53,710 --> 00:11:57,419 so let's just write EUR, which also means EURO. 214 214 00:11:57,419 --> 00:12:02,419 And so now let's see of course 215 215 00:12:02,720 --> 00:12:04,363 we didn't call the function yet. 216 216 00:12:05,210 --> 00:12:07,360 So let's do that here as well. 217 217 00:12:07,360 --> 00:12:10,670 And actually let's put this function here 218 218 00:12:10,670 --> 00:12:12,690 where it makes a little bit more sense 219 219 00:12:13,800 --> 00:12:17,693 because it kind of belongs to this display movements here. 220 220 00:12:18,955 --> 00:12:22,080 Okay. So let's actually also call this one here, 221 221 00:12:22,080 --> 00:12:25,633 calc and display balance. 222 222 00:12:26,610 --> 00:12:30,548 So to give it a similar name as that one. 223 223 00:12:30,548 --> 00:12:34,430 And so now I'm gonna call it, calc display balance. 224 224 00:12:34,430 --> 00:12:38,532 And once again, I'm calling it with account one. 225 225 00:12:38,532 --> 00:12:42,063 So account one dot movements, 226 226 00:12:43,090 --> 00:12:46,590 and then later on, as we keep developing this application 227 227 00:12:46,590 --> 00:12:49,800 of course these movements here that we're gonna use 228 228 00:12:49,800 --> 00:12:53,490 will be from the account that locked into the application. 229 229 00:12:53,490 --> 00:12:56,270 So if Jonas locked into the application, 230 230 00:12:56,270 --> 00:12:59,554 then Jonas's movements will be shown and Jonas balance. 231 231 00:12:59,554 --> 00:13:02,560 But if Steven locks in then of course 232 232 00:13:02,560 --> 00:13:06,910 Steven's movements and Steven's balance will be shown here. 233 233 00:13:06,910 --> 00:13:10,648 And right now we already see our balance up here. 234 234 00:13:10,648 --> 00:13:13,903 And so this actually happens to be exactly the same 235 235 00:13:13,903 --> 00:13:16,610 we calculated before. 236 236 00:13:16,610 --> 00:13:19,490 So also 3,840, 237 237 00:13:19,490 --> 00:13:23,453 because the movements are indeed also exactly the same. 238 238 00:13:23,453 --> 00:13:28,453 Great. So that's beautiful with this, 239 239 00:13:28,460 --> 00:13:31,776 we actually already completed some parts here 240 240 00:13:31,776 --> 00:13:34,370 of our flow chart. 241 241 00:13:34,370 --> 00:13:37,773 So let's also keep this one handy here 242 242 00:13:37,773 --> 00:13:40,163 so that we can keep looking at it. 243 243 00:13:40,163 --> 00:13:43,680 And so we already did calculating the balance 244 244 00:13:43,680 --> 00:13:45,380 and displaying the balance. 245 245 00:13:45,380 --> 00:13:47,726 So we did all this in just one function 246 246 00:13:47,726 --> 00:13:51,547 and we also already have this playing the movements. 247 247 00:13:51,547 --> 00:13:54,651 So indeed we are making some nice progress here. 248 248 00:13:54,651 --> 00:13:56,670 And the rest that you see here 249 249 00:13:56,670 --> 00:14:00,095 will be even easier as we go on. 250 250 00:14:00,095 --> 00:14:03,454 Now, just to finish this lecture here about reduce 251 251 00:14:03,454 --> 00:14:06,260 let me show you one final example 252 252 00:14:07,190 --> 00:14:09,422 because we can also do other stuff, 253 253 00:14:09,422 --> 00:14:12,090 than just adding up values 254 254 00:14:12,090 --> 00:14:15,220 that would become boring at some point, right? 255 255 00:14:15,220 --> 00:14:18,251 And so we can also do other stuff. 256 256 00:14:18,251 --> 00:14:22,780 So this time, what I want to do is to get the maximum value 257 257 00:14:24,750 --> 00:14:26,918 of the movements array here. 258 258 00:14:26,918 --> 00:14:29,880 Okay, so in this case, 259 259 00:14:29,880 --> 00:14:32,910 the result we're looking for is this 3,000. 260 260 00:14:32,910 --> 00:14:36,840 Okay. And so for that, we can also use reduce, 261 261 00:14:36,840 --> 00:14:40,688 because remember reduce is for boiling down the array 262 262 00:14:40,688 --> 00:14:43,410 into just one single value, 263 263 00:14:43,410 --> 00:14:46,100 but that value can be whatever we want. 264 264 00:14:46,100 --> 00:14:47,800 So it doesn't have to be a sum. 265 265 00:14:47,800 --> 00:14:49,654 It could be a multiplication 266 266 00:14:49,654 --> 00:14:52,160 or even something completely different, 267 267 00:14:52,160 --> 00:14:54,768 like a string or an object, 268 268 00:14:54,768 --> 00:14:57,930 but here we will keep working with numbers, 269 269 00:14:57,930 --> 00:15:00,113 but this time we want the maximum number. 270 270 00:15:01,170 --> 00:15:04,720 So let's kind of go manually through this array 271 271 00:15:04,720 --> 00:15:08,155 to determine what the reduce method should do. 272 272 00:15:08,155 --> 00:15:10,700 So let's start here at 200. 273 273 00:15:10,700 --> 00:15:13,580 Is this the maximum value at this point? 274 274 00:15:13,580 --> 00:15:16,870 Well, we don't know yet because we just started, 275 275 00:15:16,870 --> 00:15:21,520 so let's save 200 here mentally as our current maximum, 276 276 00:15:21,520 --> 00:15:25,930 then we move on to the next value and we see 450. 277 277 00:15:25,930 --> 00:15:28,800 So is this greater than our current maximum, 278 278 00:15:28,800 --> 00:15:30,860 which was 200? 279 279 00:15:30,860 --> 00:15:33,200 Well, yes it is, it is greater. 280 280 00:15:33,200 --> 00:15:37,640 And so 450 is now our current maximum 281 281 00:15:37,640 --> 00:15:41,433 then minus 400 is of course less than our current maximum. 282 282 00:15:41,433 --> 00:15:43,602 Then we move on even further. 283 283 00:15:43,602 --> 00:15:48,060 So 3,000 is a greater than our current maximum? 284 284 00:15:48,060 --> 00:15:49,260 Yes it is. 285 285 00:15:49,260 --> 00:15:52,200 And so that becomes our current maximum. 286 286 00:15:52,200 --> 00:15:54,520 So we store this 3,000 287 287 00:15:54,520 --> 00:15:56,867 and then we keep going through the array. 288 288 00:15:56,867 --> 00:16:01,867 And as you see, there is no other value greater than 3,000. 289 289 00:16:02,120 --> 00:16:06,943 And so in the end or maximum was this one, so 3,000. 290 290 00:16:08,950 --> 00:16:11,900 Okay. And now all we need to do is to 291 291 00:16:11,900 --> 00:16:14,760 essentially translate this into code, 292 292 00:16:14,760 --> 00:16:16,303 using the reduce method. 293 293 00:16:18,660 --> 00:16:23,363 So let's call it max movements to reduce. 294 294 00:16:24,670 --> 00:16:27,360 And as always, we need the accumulator 295 295 00:16:27,360 --> 00:16:29,120 and the current value. 296 296 00:16:29,120 --> 00:16:31,020 So I'm calling it movement here again. 297 297 00:16:32,320 --> 00:16:33,300 All right. 298 298 00:16:33,300 --> 00:16:36,010 And now this time, what should be the purpose 299 299 00:16:36,010 --> 00:16:38,190 of this accumulator value? 300 300 00:16:38,190 --> 00:16:40,639 So that's always the big question that we have to ask 301 301 00:16:40,639 --> 00:16:42,640 when we use reduce. 302 302 00:16:42,640 --> 00:16:46,257 So up here, when we wanted to add all the numbers together, 303 303 00:16:46,257 --> 00:16:48,600 the purpose of the accumulator 304 304 00:16:48,600 --> 00:16:51,600 was to keep track of the current sum. 305 305 00:16:51,600 --> 00:16:54,233 And so here, the accumulator will be the one 306 306 00:16:54,233 --> 00:16:57,393 that will keep track of the current maximum value. 307 307 00:16:58,270 --> 00:17:02,090 So let's just start by writing the logic here 308 308 00:17:02,090 --> 00:17:05,387 in a bigger way so we can understand what's happening. 309 309 00:17:05,387 --> 00:17:07,080 And then I will simplify it 310 310 00:17:07,080 --> 00:17:10,432 to make this a one-liner function one more time. 311 311 00:17:10,432 --> 00:17:15,288 So translating the logic that we just went over here 312 312 00:17:15,288 --> 00:17:19,588 is to say, if the accumulator is greater 313 313 00:17:19,588 --> 00:17:21,660 than the current value, 314 314 00:17:21,660 --> 00:17:26,660 which is the movement, then return the accumulator, right? 315 315 00:17:28,640 --> 00:17:30,350 Because in the reduce method, 316 316 00:17:30,350 --> 00:17:34,010 we always have to somehow return the accumulator 317 317 00:17:34,010 --> 00:17:35,860 to the next iteration. 318 318 00:17:35,860 --> 00:17:38,940 And in this case, we simply want to keep the accumulator 319 319 00:17:38,940 --> 00:17:42,332 at the value that it already is and not change it. 320 320 00:17:42,332 --> 00:17:47,332 Okay. We want to change it in the other scenario, basically. 321 321 00:17:48,310 --> 00:17:52,100 So when the current value is greater than the accumulator, 322 322 00:17:52,100 --> 00:17:54,050 so that would be this case here. 323 323 00:17:54,050 --> 00:17:57,230 So here, remember the current value is 450, 324 324 00:17:57,230 --> 00:17:59,594 but the accumulator is 200. 325 325 00:17:59,594 --> 00:18:03,529 And so now the movement is greater than accumulator. 326 326 00:18:03,529 --> 00:18:06,990 And so what we want to return here as the accumulator 327 327 00:18:06,990 --> 00:18:09,883 in the next iteration is the current movement. 328 328 00:18:12,944 --> 00:18:15,813 And so that's it, okay. 329 329 00:18:17,040 --> 00:18:19,390 And now we also need the initial value. 330 330 00:18:19,390 --> 00:18:23,480 And so the initial value make sense to be 331 331 00:18:23,480 --> 00:18:26,230 this first value here of the array. 332 332 00:18:26,230 --> 00:18:31,128 Okay. And so that's movements at position zero. 333 333 00:18:31,128 --> 00:18:33,400 Now we could have used zero here, 334 334 00:18:33,400 --> 00:18:35,540 but that would not be correct 335 335 00:18:35,540 --> 00:18:37,770 because imagine that the first value 336 336 00:18:37,770 --> 00:18:39,783 would be like a negative, 337 337 00:18:39,783 --> 00:18:42,810 then this might not work as expected. 338 338 00:18:42,810 --> 00:18:45,020 Maybe it might work with the maximum, 339 339 00:18:45,020 --> 00:18:47,334 but not with a minimum, for example. 340 340 00:18:47,334 --> 00:18:50,010 So don't just put zero here 341 341 00:18:50,010 --> 00:18:53,410 when you're trying to find a maximum or a minimum value, 342 342 00:18:53,410 --> 00:18:56,800 always just go with the first value of the array. 343 343 00:18:56,800 --> 00:18:59,480 Okay. And that's it. 344 344 00:18:59,480 --> 00:19:00,960 Let's see if it works 345 345 00:19:00,960 --> 00:19:03,393 and then we can maybe go over this again. 346 346 00:19:06,040 --> 00:19:09,670 So indeed we get 3,000. 347 347 00:19:09,670 --> 00:19:12,420 So we started at 200. 348 348 00:19:12,420 --> 00:19:13,950 So in this iteration, 349 349 00:19:13,950 --> 00:19:17,040 is the accumulator greater than the movement? 350 350 00:19:17,040 --> 00:19:18,690 Well, it is not. 351 351 00:19:18,690 --> 00:19:23,300 And so return the current movement. Okay. 352 352 00:19:23,300 --> 00:19:25,245 But the first iteration is not that important. 353 353 00:19:25,245 --> 00:19:28,570 It gets interesting here in the second one. 354 354 00:19:28,570 --> 00:19:31,090 So now our accumulator is at 200 355 355 00:19:31,090 --> 00:19:34,360 and the current movement is at 450. 356 356 00:19:34,360 --> 00:19:36,970 So this one here is false again. 357 357 00:19:36,970 --> 00:19:39,190 And so now we return to movement 358 358 00:19:39,190 --> 00:19:42,220 as the new accumulator in the next iteration. 359 359 00:19:42,220 --> 00:19:44,710 All right. And so in the next iteration, 360 360 00:19:44,710 --> 00:19:47,289 that accumulator is 450 now, 361 361 00:19:47,289 --> 00:19:52,289 then here, the current movement is minus 400. 362 362 00:19:52,540 --> 00:19:57,540 And so here is 450 greater than minus 400. 363 363 00:19:57,830 --> 00:19:59,530 Yes, of course it is. 364 364 00:19:59,530 --> 00:20:01,840 And so let's simply return the accumulator 365 365 00:20:01,840 --> 00:20:04,970 so that it stays the same in the next iteration. 366 366 00:20:04,970 --> 00:20:06,910 And so that's the logic all the way 367 367 00:20:06,910 --> 00:20:08,800 until the end of the array. 368 368 00:20:08,800 --> 00:20:12,820 And, by this, we then end up with this maximum value. 369 369 00:20:12,820 --> 00:20:16,320 All right. So I hope that made sense. 370 370 00:20:16,320 --> 00:20:18,610 And in fact, there is a ton of things 371 371 00:20:18,610 --> 00:20:21,550 that we can do with this reduce method. 372 372 00:20:21,550 --> 00:20:24,863 It is by far the most powerful array method there is. 373 373 00:20:26,380 --> 00:20:27,490 And because of that, 374 374 00:20:27,490 --> 00:20:30,180 it can also be the hardest one to use. 375 375 00:20:30,180 --> 00:20:33,070 So we always need to think exactly what we want 376 376 00:20:33,070 --> 00:20:36,170 the accumulator and the core value to be 377 377 00:20:36,170 --> 00:20:37,765 and how they should interact. 378 378 00:20:37,765 --> 00:20:40,807 But I hope that with this exercise here 379 379 00:20:40,807 --> 00:20:44,210 I could demonstrate the thought process a little bit 380 380 00:20:44,210 --> 00:20:47,290 and throughout the section and the rest of the course, 381 381 00:20:47,290 --> 00:20:50,890 there will be some more exercises of the reduce method 382 382 00:20:50,890 --> 00:20:53,250 so that you can also learn better and better 383 383 00:20:53,250 --> 00:20:55,263 how to use it yourself one day. 33224

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