WEBVTT

1
00:00:01.440 --> 00:00:04.520
<v Jonas>Welcome to Coding Challenge number three.</v>

2
00:00:04.520 --> 00:00:07.293
And this is gonna be a quick and easy one.

3
00:00:09.260 --> 00:00:12.130
So all I want you to do is to rewrite

4
00:00:12.130 --> 00:00:14.906
the 'calcAverageHumanAge' function

5
00:00:14.906 --> 00:00:17.870
that we wrote in the previous challenge.

6
00:00:17.870 --> 00:00:20.500
Now the difference here is that this time,

7
00:00:20.500 --> 00:00:22.530
you should use an arrow function

8
00:00:22.530 --> 00:00:25.140
and you should use chaining.

9
00:00:25.140 --> 00:00:27.170
Now to make this chaining work,

10
00:00:27.170 --> 00:00:29.700
just keep in mind the second way that I used

11
00:00:29.700 --> 00:00:32.970
in the last challenge to calculate the average.

12
00:00:32.970 --> 00:00:35.710
Then, as always, use this test data here

13
00:00:35.710 --> 00:00:38.090
to try out your function.

14
00:00:38.090 --> 00:00:40.010
And so I'll see you here in a minute,

15
00:00:40.010 --> 00:00:42.793 line:15% 
or five minutes maybe, once you're done.

16
00:00:46.030 --> 00:00:48.890
Now all right, so let's grab the code here

17
00:00:48.890 --> 00:00:50.313
from the previous one.

18
00:00:51.960 --> 00:00:54.953
So there's no need to just rewrite everything.

19
00:00:58.320 --> 00:01:00.800
So let's get rid of all the comments here,

20
00:01:00.800 --> 00:01:02.273
all the console.logs.

21
00:01:03.910 --> 00:01:05.970
And so let's save it here,

22
00:01:05.970 --> 00:01:08.623
and then rewrite it as an arrow function.

23
00:01:13.000 --> 00:01:14.963
So it still takes in the ages,

24
00:01:16.370 --> 00:01:20.240
and now what it returns is simply the ages,

25
00:01:20.240 --> 00:01:22.053
then dot, this map,

26
00:01:23.370 --> 00:01:25.703
so calculating the human ages.

27
00:01:26.790 --> 00:01:29.810
Okay, and then on the result of that,

28
00:01:29.810 --> 00:01:33.930
we filter for only the adult docs.

29
00:01:33.930 --> 00:01:36.253
So when the age is greater than 18.

30
00:01:37.130 --> 00:01:39.003
So this is what we have for now.

31
00:01:40.040 --> 00:01:41.753
Let's call this one here two.

32
00:01:43.550 --> 00:01:48.130
Okay. And so for now this is the two erase that we have

33
00:01:48.130 --> 00:01:49.940
one for each function call.

34
00:01:49.940 --> 00:01:53.740
But now let's then also calculate the average.

35
00:01:53.740 --> 00:01:56.770
And so for that all we need to do is to chain

36
00:01:56.770 --> 00:01:59.580
this method of calculating the average.

37
00:01:59.580 --> 00:02:01.740
That he showed you in the last lecture,

38
00:02:01.740 --> 00:02:03.440
or actually in the last challenge.

39
00:02:04.780 --> 00:02:06.473
So pasting that here.

40
00:02:07.520 --> 00:02:11.300
And so now we get the exact same results as before,

41
00:02:11.300 --> 00:02:13.660
and that's actually already it.

42
00:02:13.660 --> 00:02:15.950
Now what's really important to notice here,

43
00:02:15.950 --> 00:02:19.820
is that this is actually really the only way

44
00:02:19.820 --> 00:02:22.300
of calculating the average now.

45
00:02:22.300 --> 00:02:25.180
So basically dividing the age by the length

46
00:02:25.180 --> 00:02:27.930
of the current array immediately here

47
00:02:27.930 --> 00:02:30.120
in each of the iteration.

48
00:02:30.120 --> 00:02:32.400
So remember that in the last challenge,

49
00:02:32.400 --> 00:02:35.380
I first simply added all the values together.

50
00:02:35.380 --> 00:02:39.580
And then afterwards I divided it by the length of the array.

51
00:02:39.580 --> 00:02:42.760
So in this case, by the length of the adults array.

52
00:02:42.760 --> 00:02:45.660
However, that would not work in this case.

53
00:02:45.660 --> 00:02:48.620
Because there would be no way of knowing the length

54
00:02:48.620 --> 00:02:50.300
of the adults array.

55
00:02:50.300 --> 00:02:53.100
So of this result here, right?

56
00:02:53.100 --> 00:02:55.180
Because we haven't stored it anyway.

57
00:02:55.180 --> 00:03:00.180
And so we couldn't do like this, like adults.length, right?

58
00:03:01.990 --> 00:03:03.340
This wouldn't be possible

59
00:03:03.340 --> 00:03:07.090
because adults here does not exist anywhere.

60
00:03:07.090 --> 00:03:09.930
And therefore we need to take that length

61
00:03:09.930 --> 00:03:12.120
from this input here.

62
00:03:12.120 --> 00:03:14.050
So from this fourth parameter,

63
00:03:14.050 --> 00:03:16.010
which gives us access to the array,

64
00:03:16.010 --> 00:03:18.770
on which the method is called.

65
00:03:18.770 --> 00:03:21.390
And that's the reason why I showed you this way here,

66
00:03:21.390 --> 00:03:22.870
in the last lecture.

67
00:03:22.870 --> 00:03:25.680
Because yeah, again this is the only way

68
00:03:25.680 --> 00:03:29.140
in which we can now calculate this average,

69
00:03:29.140 --> 00:03:31.470
unless we wanted to do it in two steps.

70
00:03:31.470 --> 00:03:32.770
But my instruction here

71
00:03:32.770 --> 00:03:35.930
was to really do it all with chaining.

72
00:03:35.930 --> 00:03:37.913
And so this is the way to go then.

73
00:03:39.330 --> 00:03:41.590
All right, so make sure you understand

74
00:03:41.590 --> 00:03:45.000
really why and also how this one here works.

75
00:03:45.000 --> 00:03:48.186
And then let's already move on to our next method,

76
00:03:48.186 --> 00:03:51.020
which is gonna be defined method.

77
00:03:51.020 --> 00:03:52.780
So another really cool one.

78
00:03:52.780 --> 00:03:54.623
So I can't wait to see you there.

