1
00:00:00,110 --> 00:00:01,220
OK we'll come back.

2
00:00:01,260 --> 00:00:05,940
As I mentioned in the last video we have one more exercise just to get some more practice with the command

3
00:00:05,940 --> 00:00:11,490
line and running files with node as well as brushing up some of those basic javascript skills.

4
00:00:11,490 --> 00:00:13,470
So again I've typed up the instructions.

5
00:00:13,470 --> 00:00:15,660
Feel free to read them and just go over it on your own.

6
00:00:15,780 --> 00:00:18,150
But also walk through them step by step.

7
00:00:18,180 --> 00:00:22,100
So the first thing you need to do is create a new file called Greater Dot.

8
00:00:22,170 --> 00:00:22,940
Yes.

9
00:00:23,010 --> 00:00:27,540
The idea behind this exercise is that you are a teacher teaching a class and you have a bunch of test

10
00:00:27,540 --> 00:00:32,340
scores and they're in an array and you want to find the average score in the array.

11
00:00:32,490 --> 00:00:39,000
So you're going to define a new function called Average inside if the new file and that average function

12
00:00:39,000 --> 00:00:46,080
should take a single parameter an array of test scores which are all numbers between 0 and 100 and then

13
00:00:46,080 --> 00:00:49,380
the function should return the average score in the array.

14
00:00:49,650 --> 00:00:53,020
But it should be rounded to the nearest whole number so no.

15
00:00:53,030 --> 00:00:55,340
Ninety three point three four to one.

16
00:00:55,440 --> 00:00:56,350
Blah blah blah.

17
00:00:56,400 --> 00:00:58,680
It should just be 93.

18
00:00:58,680 --> 00:01:03,500
Here are two examples that you should copy into your code and make sure that they work.

19
00:01:03,510 --> 00:01:06,470
So the first one we have some scores that are a little higher.

20
00:01:06,510 --> 00:01:13,380
And the second one some more negative scores and the first one should return 94 and the second one should

21
00:01:13,380 --> 00:01:15,080
return 68.

22
00:01:15,300 --> 00:01:17,660
Just a hint about rounding to the nearest whole number.

23
00:01:17,730 --> 00:01:21,530
You don't need to do that yourself there's no fancy logic you need to do.

24
00:01:21,570 --> 00:01:25,070
There's actually a special method on the math object that will help you.

25
00:01:25,400 --> 00:01:28,670
So do a little research on that as always puzzle video.

26
00:01:28,710 --> 00:01:33,920
Give this a shot and I'll be back with a solution.

27
00:01:36,900 --> 00:01:38,460
OK let's get started.

28
00:01:38,460 --> 00:01:45,170
First of all we need to make a new file called Greater us just like that.

29
00:01:45,300 --> 00:01:46,570
Make sure it worked.

30
00:01:46,590 --> 00:01:55,620
There it is greater dot J us and then we need to define a function average and average should take an

31
00:01:55,620 --> 00:01:56,810
array of scores.

32
00:01:56,850 --> 00:02:04,200
So we'll just call it scores and I'm going to copy these two test cases to the very bottom just so I

33
00:02:04,200 --> 00:02:05,170
have them.

34
00:02:05,790 --> 00:02:11,310
So we pass in an array of numbers and to find the average of something we're going to need to take every

35
00:02:11,310 --> 00:02:17,640
element and added together and then we'll need to divide by the number of elements so that can be first

36
00:02:18,540 --> 00:02:26,190
add all scores together and then next would be divide by total number of scores.

37
00:02:26,190 --> 00:02:35,190
And then lastly round OK to add all the scores together we need to loop through the array and have a

38
00:02:35,190 --> 00:02:37,830
variable that we're adding each number to.

39
00:02:37,980 --> 00:02:43,350
And this is something called the accumulator pattern where we have a variable that is going to accumulate

40
00:02:43,350 --> 00:02:46,500
something as we iterate through an array or collection.

41
00:02:46,680 --> 00:02:49,140
So in this case it will accumulate a total.

42
00:02:49,140 --> 00:02:53,290
So we'll find a variable called Total and started at zero.

43
00:02:53,310 --> 00:02:55,310
Then we loop through scores.

44
00:02:55,320 --> 00:02:57,640
So to do that we could use a for loop a while loop.

45
00:02:57,780 --> 00:02:59,670
Or my favorite A for each.

46
00:02:59,730 --> 00:03:08,430
So I'll do a for each scores dot for each function and we'll just call it score as our placeholder variable

47
00:03:08,430 --> 00:03:09,210
.

48
00:03:09,210 --> 00:03:16,230
Each one of these is going to be named score and then we'll just do a total plus equals score.

49
00:03:16,860 --> 00:03:20,450
So this would work to go through the array and it adds everything together.

50
00:03:20,460 --> 00:03:25,920
The next thing that we need to do is divide by the total number of scores to do that.

51
00:03:25,920 --> 00:03:34,320
Let's make another variable called Average HEG equals total divided by and the number of scores is just

52
00:03:34,440 --> 00:03:36,410
scores of length.

53
00:03:37,620 --> 00:03:43,340
And then the last thing here is to round it which is math dot round.

54
00:03:44,010 --> 00:03:51,750
And we want to round a Viji and return that just like that math that round is one of those built in

55
00:03:51,750 --> 00:03:55,740
methods like math floor or math dot Rand.

56
00:03:55,800 --> 00:04:01,350
And unlike math top floor which we'll just chop off the decimal point math that round we'll round up

57
00:04:01,350 --> 00:04:03,500
or down depending on the decimal.

58
00:04:03,990 --> 00:04:05,590
OK so let's work through this again.

59
00:04:05,880 --> 00:04:07,230
We pass in an array.

60
00:04:07,320 --> 00:04:08,890
It's called scores.

61
00:04:09,070 --> 00:04:11,180
We start a total variable at zero.

62
00:04:11,190 --> 00:04:16,040
We loop through the scores using a for each in this case but we could use any any other type of loop

63
00:04:16,050 --> 00:04:16,360
.

64
00:04:16,770 --> 00:04:22,800
And then we add each score into the total and then we divide the total by all the scores scores that

65
00:04:22,800 --> 00:04:30,150
length and that set that equal to the variable average and then we math round average and then we return

66
00:04:30,150 --> 00:04:30,840
that.

67
00:04:31,260 --> 00:04:40,210
So if we run this now save and rerun node that greater or node greater dot J Yes we have a small problem

68
00:04:40,390 --> 00:04:45,050
not with our logic but in that we have no idea if this worked.

69
00:04:45,210 --> 00:04:47,830
And that's of course because we're only returning something.

70
00:04:48,240 --> 00:04:50,590
I never told you to actually print anything out.

71
00:04:50,610 --> 00:04:53,400
So what we could do is either cancel that log.

72
00:04:53,400 --> 00:04:55,910
This here that's probably the best way.

73
00:04:56,130 --> 00:04:57,950
So that we don't actually change our function.

74
00:04:57,960 --> 00:05:00,870
It still returns and we'll just cancel that log.

75
00:05:00,870 --> 00:05:03,630
The result just like that.

76
00:05:04,110 --> 00:05:05,410
There's the first one.

77
00:05:05,970 --> 00:05:06,980
And now a constant log.

78
00:05:06,980 --> 00:05:13,380
The second one and we're hoping to get 94 and 68 and let's add a message here to make it clear so Consta

79
00:05:13,390 --> 00:05:22,590
that log this will be average score for and phi and mental science which I took it was extremely easy

80
00:05:22,590 --> 00:05:22,620
.

81
00:05:22,620 --> 00:05:25,160
The average was probably higher than 94.

82
00:05:25,560 --> 00:05:26,890
And then I'll do another one here.

83
00:05:26,910 --> 00:05:30,090
Cancel that log average score.

84
00:05:30,270 --> 00:05:37,290
And this will be for organic chemistry where the average score on a test was probably much lower than

85
00:05:37,290 --> 00:05:38,380
68.

86
00:05:38,490 --> 00:05:45,300
So let's save and now run that again just hit the up arrow and hit enter and we get 94 which is what

87
00:05:45,300 --> 00:05:46,200
we expected.

88
00:05:46,200 --> 00:05:50,050
And 68 which is also what we expected.

89
00:05:50,070 --> 00:05:52,040
So that's it for these quick exercises.

90
00:05:52,050 --> 00:05:56,930
The next thing we're going to do is learn about something called NPM node package manager.

91
00:05:57,030 --> 00:06:02,160
And that will put us one step closer to writing our server side logic and making full stack web applications

92
00:06:02,160 --> 00:06:02,430
.

93
00:06:02,430 --> 00:06:07,020
Right now we're just writing these simple functions instead of a file and running them.

94
00:06:07,020 --> 00:06:11,640
But once we learn more about NPM and talk about some of the packages we're going to use we actually

95
00:06:11,640 --> 00:06:13,710
start writing full applications.
