1
00:00:00,330 --> 00:00:04,620
Let me show you how I'm going to solve this challenge.

2
00:00:04,680 --> 00:00:05,580
Here you go.

3
00:00:05,790 --> 00:00:10,360
So first one is to write a function that takes away.

4
00:00:10,380 --> 00:00:12,780
So let's solve the first one.

5
00:00:12,780 --> 00:00:14,670
So here is my function.

6
00:00:15,030 --> 00:00:20,210
Let me use function expression system around any bigger so that I can see it.

7
00:00:21,440 --> 00:00:21,990
Okay.

8
00:00:22,440 --> 00:00:27,570
So const is going to average.

9
00:00:29,480 --> 00:00:39,200
Numbers or average now is equal to my function and that so this function we take in an array and then

10
00:00:39,200 --> 00:00:41,660
my parenthesis inside.

11
00:00:41,690 --> 00:00:42,560
Let's see.

12
00:00:43,550 --> 00:00:48,190
The function should take an array and return the the average.

13
00:00:48,200 --> 00:00:49,400
So here we go.

14
00:00:49,730 --> 00:00:52,280
So how can we get an average?

15
00:00:52,280 --> 00:01:02,420
For example, if we have these records, let's say we have a ray of let's say four, comma, five,

16
00:01:02,630 --> 00:01:11,030
comma, six, and then come on to, like I said, we're going to add these together, four plus five

17
00:01:11,030 --> 00:01:22,280
plus six plus two over two and over two over 1 to 3 for the number of items present in the array.

18
00:01:23,000 --> 00:01:28,280
So in the record as that's if I do this one, I'm able to get my average.

19
00:01:28,280 --> 00:01:32,600
So how can I or how can you write some code to do something like this one?

20
00:01:32,600 --> 00:01:37,370
Remember, for this addition here we can use reduce.

21
00:01:38,680 --> 00:01:43,520
And to be able to get the total length we can use length method.

22
00:01:43,540 --> 00:01:50,260
So we need reduce method and then the length method to get it done, as simple as that.

23
00:01:50,350 --> 00:01:51,380
So let's see.

24
00:01:51,400 --> 00:01:55,150
So let's start with the first the second one.

25
00:01:56,020 --> 00:01:56,620
Okay.

26
00:01:56,680 --> 00:01:58,210
Yeah, yeah.

27
00:01:58,210 --> 00:02:01,150
That's the function should be taken away and retain the average.

28
00:02:01,150 --> 00:02:06,250
So here I would say calculate the average.

29
00:02:06,730 --> 00:02:07,540
Here we go.

30
00:02:07,990 --> 00:02:09,850
So cost.

31
00:02:10,600 --> 00:02:15,430
I would say the average is equal to the array.

32
00:02:15,430 --> 00:02:18,340
Our pass in dot reduce.

33
00:02:19,470 --> 00:02:23,530
And then inside I'll pass in my callback function.

34
00:02:23,550 --> 00:02:31,140
I have access to my accumulator and my current value as that, remember, because you talk about it

35
00:02:31,140 --> 00:02:36,330
before and now my parentheses, and that's why my initial value is that.

36
00:02:36,330 --> 00:02:38,160
So let me expand this.

37
00:02:39,330 --> 00:02:46,890
So here I'm going to return return the accumulator.

38
00:02:47,670 --> 00:02:48,780
Plus.

39
00:02:49,670 --> 00:02:57,830
The agreement applies the current value, not EU, but when you here are here wherever.

40
00:02:57,830 --> 00:03:01,570
So as current value as that.

41
00:03:01,580 --> 00:03:03,980
So I got it right.

42
00:03:04,100 --> 00:03:06,730
So next is I need to return from this function.

43
00:03:06,740 --> 00:03:08,990
So to return the average I got here.

44
00:03:08,990 --> 00:03:12,770
So let's say return the result.

45
00:03:15,270 --> 00:03:18,060
Results from the function.

46
00:03:19,980 --> 00:03:21,000
So here you go.

47
00:03:21,090 --> 00:03:24,090
Return the average.

48
00:03:24,830 --> 00:03:27,100
And remember, you convert this one.

49
00:03:27,110 --> 00:03:28,640
Okay, I'll show you what I mean here.

50
00:03:28,670 --> 00:03:32,820
So next is let's call it and see how we can solve the next one.

51
00:03:32,900 --> 00:03:33,620
We are done.

52
00:03:33,620 --> 00:03:39,500
We are done with these two, as is this one and this one for the meantime, let's try our code first.

53
00:03:39,500 --> 00:03:45,440
So it comes, let's say answer is equal to our call.

54
00:03:45,470 --> 00:03:50,450
My function is average avg number.

55
00:03:50,480 --> 00:03:53,780
Then I'll pass in any array I think I have.

56
00:03:55,090 --> 00:03:55,890
The amount.

57
00:03:55,900 --> 00:04:00,910
Yeah, let me let me comment out this one because I have the verbal name being declared down here.

58
00:04:01,540 --> 00:04:01,990
Yes.

59
00:04:01,990 --> 00:04:04,290
So I'll pass in an array.

60
00:04:04,300 --> 00:04:06,950
So let's say I'll pass in in line here.

61
00:04:06,970 --> 00:04:10,810
Let's say I have two, three, four, five.

62
00:04:11,200 --> 00:04:13,840
So let's console.log.

63
00:04:13,840 --> 00:04:15,180
Console.log.

64
00:04:15,190 --> 00:04:16,390
The answer.

65
00:04:17,170 --> 00:04:20,649
Now we've got for let's see if we are right.

66
00:04:20,980 --> 00:04:23,530
Two plus three is five.

67
00:04:23,530 --> 00:04:26,710
Plus four is nine plus five is 14.

68
00:04:26,710 --> 00:04:28,030
Now, we got it.

69
00:04:28,660 --> 00:04:32,220
So but the question says we should find the average.

70
00:04:32,230 --> 00:04:40,450
So here in divide this one, divide this one by OC for the meantime, before we do that, what will

71
00:04:40,460 --> 00:04:42,490
the average let's do it manually.

72
00:04:42,670 --> 00:04:50,680
We have to this is 14 right divided by let's say we have two, one, two, three, four.

73
00:04:50,680 --> 00:04:58,960
So 14 here let me have we're going to have 14 over four.

74
00:04:59,440 --> 00:05:01,390
What would be the answer for this one?

75
00:05:01,750 --> 00:05:03,520
Well, let's see.

76
00:05:03,850 --> 00:05:10,240
So next is and divide the number of record here, the number of items in the record.

77
00:05:10,240 --> 00:05:16,270
And I can get it as what this array length which is coming from this.

78
00:05:16,810 --> 00:05:18,400
So this array is this one.

79
00:05:18,400 --> 00:05:25,990
So I can append over, let's say divided by the array dot length.

80
00:05:27,700 --> 00:05:29,380
And that is if I save it.

81
00:05:29,410 --> 00:05:30,400
Let's check the answer.

82
00:05:30,430 --> 00:05:33,580
Now we got 3.5.

83
00:05:34,430 --> 00:05:40,480
You see, the last thing is you should convert it to two decimal places because.

84
00:05:41,140 --> 00:05:42,490
Okay, let's ask maybe more.

85
00:05:42,610 --> 00:05:43,960
Let's see, we have nine.

86
00:05:43,960 --> 00:05:44,930
We have two.

87
00:05:44,950 --> 00:05:45,820
Let's see.

88
00:05:46,120 --> 00:05:50,590
We see that we got some random numbers, but I want to run up to two decimal places.

89
00:05:50,590 --> 00:05:52,990
So we need to use what is called fix.

90
00:05:52,990 --> 00:05:59,100
So on the result here we can use to fix to fix.

91
00:05:59,110 --> 00:06:02,550
Remember the arithmetic we are combining all together.

92
00:06:02,560 --> 00:06:04,390
I'll pass in as two.

93
00:06:04,870 --> 00:06:14,890
So let's say with this one now see that I got 4.12 to 2 decimal places, but it says that we should

94
00:06:14,890 --> 00:06:18,160
return in a format that we don't assign.

95
00:06:18,160 --> 00:06:19,240
So here.

96
00:06:20,120 --> 00:06:25,100
I can remove this one, and now I can inject some temperature here.

97
00:06:25,130 --> 00:06:26,240
Now I say that.

98
00:06:27,200 --> 00:06:30,230
Well, I should add a dollar sign.

99
00:06:30,230 --> 00:06:30,470
Right?

100
00:06:30,470 --> 00:06:33,980
So here I will add my dollar sign like that.

101
00:06:34,190 --> 00:06:36,590
And next is I want to add dynamic value.

102
00:06:36,620 --> 00:06:41,370
Dollar sign sees it now paste the results, then remove the semicolon here.

103
00:06:41,390 --> 00:06:46,160
Let's see now I have $4.17.

104
00:06:46,820 --> 00:06:48,390
That is it, guys.

105
00:06:48,410 --> 00:06:49,430
The next video.

106
00:06:49,910 --> 00:06:50,780
Let's see.

107
00:06:50,780 --> 00:06:52,460
Another coding challenge.

