OK we'll come back.
As I mentioned in the last video we have one more exercise just to get some more practice with the command
line and running files with node as well as brushing up some of those basic javascript skills.
So again I've typed up the instructions.
Feel free to read them and just go over it on your own.
But also walk through them step by step.
So the first thing you need to do is create a new file called Greater Dot.
Yes.
The idea behind this exercise is that you are a teacher teaching a class and you have a bunch of test
scores and they're in an array and you want to find the average score in the array.
So you're going to define a new function called Average inside if the new file and that average function
should take a single parameter an array of test scores which are all numbers between 0 and 100 and then
the function should return the average score in the array.
But it should be rounded to the nearest whole number so no.
Ninety three point three four to one.
Blah blah blah.
It should just be 93.
Here are two examples that you should copy into your code and make sure that they work.
So the first one we have some scores that are a little higher.
And the second one some more negative scores and the first one should return 94 and the second one should
return 68.
Just a hint about rounding to the nearest whole number.
You don't need to do that yourself there's no fancy logic you need to do.
There's actually a special method on the math object that will help you.
So do a little research on that as always puzzle video.
Give this a shot and I'll be back with a solution.
OK let's get started.
First of all we need to make a new file called Greater us just like that.
Make sure it worked.
There it is greater dot J us and then we need to define a function average and average should take an
array of scores.
So we'll just call it scores and I'm going to copy these two test cases to the very bottom just so I
have them.
So we pass in an array of numbers and to find the average of something we're going to need to take every
element and added together and then we'll need to divide by the number of elements so that can be first
add all scores together and then next would be divide by total number of scores.
And then lastly round OK to add all the scores together we need to loop through the array and have a
variable that we're adding each number to.
And this is something called the accumulator pattern where we have a variable that is going to accumulate
something as we iterate through an array or collection.
So in this case it will accumulate a total.
So we'll find a variable called Total and started at zero.
Then we loop through scores.
So to do that we could use a for loop a while loop.
Or my favorite A for each.
So I'll do a for each scores dot for each function and we'll just call it score as our placeholder variable
.
Each one of these is going to be named score and then we'll just do a total plus equals score.
So this would work to go through the array and it adds everything together.
The next thing that we need to do is divide by the total number of scores to do that.
Let's make another variable called Average HEG equals total divided by and the number of scores is just
scores of length.
And then the last thing here is to round it which is math dot round.
And we want to round a Viji and return that just like that math that round is one of those built in
methods like math floor or math dot Rand.
And unlike math top floor which we'll just chop off the decimal point math that round we'll round up
or down depending on the decimal.
OK so let's work through this again.
We pass in an array.
It's called scores.
We start a total variable at zero.
We loop through the scores using a for each in this case but we could use any any other type of loop
.
And then we add each score into the total and then we divide the total by all the scores scores that
length and that set that equal to the variable average and then we math round average and then we return
that.
So if we run this now save and rerun node that greater or node greater dot J Yes we have a small problem
not with our logic but in that we have no idea if this worked.
And that's of course because we're only returning something.
I never told you to actually print anything out.
So what we could do is either cancel that log.
This here that's probably the best way.
So that we don't actually change our function.
It still returns and we'll just cancel that log.
The result just like that.
There's the first one.
And now a constant log.
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
that log this will be average score for and phi and mental science which I took it was extremely easy
.
The average was probably higher than 94.
And then I'll do another one here.
Cancel that log average score.
And this will be for organic chemistry where the average score on a test was probably much lower than
68.
So let's save and now run that again just hit the up arrow and hit enter and we get 94 which is what
we expected.
And 68 which is also what we expected.
So that's it for these quick exercises.
The next thing we're going to do is learn about something called NPM node package manager.
And that will put us one step closer to writing our server side logic and making full stack web applications
.
Right now we're just writing these simple functions instead of a file and running them.
But once we learn more about NPM and talk about some of the packages we're going to use we actually
start writing full applications.