Instructor: Welcome to the first coding challenge
of this section.
And as you can imagine,
this one is gonna be all about functions.
And we're back to the two gymnastic teams
from the previous section, the Dolphins and Koalas.
And now there is a new gymnastics discipline
which works in a bit different way.
So now in this new discipline,
each team competes three times
and then the average of the three scores is calculated,
so each team will get one average score.
And then a team only wins
if it has at least doubled the average score
of the other team and otherwise no team wins.
So sounds pretty simple, I hope.
And let's take a look at your tasks now.
So first create an arrow function
called calcAverage which calculates the average
of three scores.
And actually this calcAverage function
should be able to calculate the average of any three values.
So of course, the values don't have to be scores.
Any random numbers will work just the same.
So this is basically a generic function
just to calculate the average of three values.
Then use that function
to calculate the average for both teams.
And again, down here,
you have the test data that you can use.
Create a function called checkWiner
which takes the average score of each team as parameters,
and these parameters
should be called average Dolphins here and average Koalas.
And then based on these two average scores,
the function will log the winner to the console
together with the victory points.
And that of course needs to happen according to the rule
that I just explained before.
So a team only wins
if it has at least doubled the average score
of the other team,
and otherwise no team wins.
So you need to check for that condition
in order to print which team wins,
and in some cases, no team might win at all.
So you need to account for that as well.
So here in point three you write to checkWiner function
and then a number four you will use.
So basically you will call the checkWinner function
in order to determine the winner
for both to test data one and test data two.
I have some hints again,
but if you want to try it on your own without the hints
then just past the video right now.
But if you need to hints
then well, here they go.
So this one is probably pretty obvious
which is to calculate the average of three values,
add them altogether and divide by three.
But the second one here is a little bit more important
and maybe more difficult for you to figure out on your own.
So to check if number A is at least double number B,
what you need to check is if A
is greater or equal two times B.
And so this is what you will use
with the teams average scores.
So it's important to keep this one in mind
because of this rule,
that the team only wins when it has doubled average score
of the other team.
Now, please make really sure
that you understood everything I showed you about functions
before you attempt this challenge.
Because this one is not so straight forward,
and so I don't want you to get frustrated.
So again, please really make sure you understand functions
and only then attempt to do this challenge.
But if you feel that you're ready,
then just pause the video now
and good luck with this challenge.
Let me show you my solution to this challenge,
which I hope of course you were able to do on your own.
So I'm gonna start
with the calcAverage function as a number one,
and remember that this should be an arrow function
and it will receive three values
so that it can calculate the average
of these three numbers.
And here I'm actually just gonna call
the three numbers, A, B and C,
because again, this is supposed to be just
a generic function which can calculate averages
of any three numbers,
it doesn't have to be any scores.
And so A, B, C are just generic variable names.
But of course it's totally okay
if you used more specific variable names.
So adding all the three numbers together
and then dividing by three.
So this one is nothing new,
this was just to basically test your knowledge
of the syntax of the arrow function.
So what's important to notice here
is that we need to parenthesis here
because we have multiple parameters,
but then here, we only really have this line of code.
And so therefore we don't need to even write the return.
This value gets automatically returned from the function.
And now let's just test it out
before we move any further.
So calcAverage and then let's just use some random numbers
because here we know that this is supposed to be four.
So we have three, four, five.
And so the middle one here
is the average between these three.
And indeed it is, and so we know that our function works.
And so we can move on to point number two,
which is to use the function
to calculate the average for both teams.
So let's say score Dolphins
is equal to calcAverage
and now we pass into specific arguments
for the three parameters, A, B and C.
And so for test data one, which is this,
so let me actually write that here, test one.
So for the Dolphins, we pass into values 44, 23 and 71.
Then for the Koalas, it works the same way.
So score Koalas equals calcAverage.
And now we have 65, 54 and 49.
Nice reusable function right here.
And so having this function,
we don't need to calculate it here manually
every single time.
So that's really great.
We can really appreciate the fact that again,
a function is really like a machine in some sense.
So it doesn't care where the inputs come from
and where they go.
All it does is to calculate the average of three numbers,
no matter if it's scores or heights of people
or money or whatever it is.
So this function really is kind of isolated
from the rest of the code here.
It's a standalone function that we can then use
for our own purposes.
And that might sound pretty obvious,
but it's actually important that you really internalize
this aspect of functions.
Only then can you really start
to see the potential for functions everywhere in your code.
Anyway, with that being said,
let's now log these values to the console
'cause I always like to see what I'm doing.
So this part is of course not mandatory,
but it's also nice to be able to know the scores
so that we know if the logic
that we're gonna implement next is working correctly.
So we see that in this case,
the Koalas have a score that is higher
than one of the Dolphins but it's not double.
And so we already know that in this case
no team should be able to win,
but let's not actually implement that functionality.
So that's the function checkWinner.
And so it takes the average Dolphins
and average Koalas parameters
and then it locks the winner to the console.
So let's do that.
And what's more, it's no problem
if you did this differently.
If you made it work then it's great.
So average Dolphins and average Koalas.
And now all they need to do is to check
whether the Dolphins won or the Koalas won
or if no one won basically.
So when do the Dolphins win?
Well, they win
if they have doubled the points of the Koalas,
that's the condition.
So again, a team only wins
if it has at least doubled the average score
of the other team.
And so that's why I told you here in this hint
that this is how we check for that.
So A has double the amount of points of B
if A is greater than two times B.
So let's do that.
So average Dolphins greater
or equal to two times the average Koalas.
And in this case, we look to the console
Dolphins win the trophy,
let's use an emoji here again
but you don't need of course to do that.
So it's already here,
and now it also told us to put the points.
For example, 30 versus 13,
so both points with the versus in between.
So that's average Dolphins
versus average Koalas.
So you'll see that here
we are also practicing all the other skills
that we already acquired before.
Especially in the previous section.
Then now here we need to just need to do the opposite.
So else if,
and now if the Koalas half at least.
So that's basically what this one here means.
So greater equal is basically at least.
So if average Koalas
have at least double the points of the Dolphins,
then the Koalas win.
So that's just grab this one from here.
Koalas and tier it's average Koalas,
and then average Dolphins, average, yes.
And now in any other case,
we say that no one wins.
So I think that's also said in the rules.
Yeah, that's a number of five.
I think I even forgot to tell you this one
but hopefully you read it.
So you should ignore draws in this exercise this time.
So if none of these is true,
we simply say that no team wins.
No team wins.
And now all we need to do is to call this function here.
So checkWinner, and we will do that
with score Dolphins
and score Koalas.
Let's recap, we calculated the score of the Dolphins up here
and of the Koalas as well.
And now we call the checkWiner function here
and we use as arguments
the scores that we calculated before
and then score Dolphins will become average Dolphins
here in this function,
because that's the name of the parameter.
And I see that I misspelled this,
and so now I can use that command D
that I talked to you about before,
so I can select all of them.
So command D, command D,command D, command D
and now I can use all of these cursors here
at the same time.
So Dolphins you see,
I inserted the P in all of the variable names everywhere.
And so now this parameter name here gets used
for all these calculations.
And again, originally this value comes basically from here,
from calculating the average of these three scores.
So this average gets stored here
then we use it here
and then it gets basically transferred to average Dolphins
and is then used everywhere in this function.
So let's now test.
And as we expected, no team wins
because this team here
even though it has more than the Dolphins, it's not double.
And so therefore they do not win.
Now of course, what is also important to mention here
is that this checkWiner function here
is also completely independent of these score values
that we calculated earlier.
So here we can simply call this function
with completely different values.
So the function doesn't care
where do these values come from.
So where average Dolphins and average Koalas come from.
It's a standalone function
and it doesn't care at all, where it is values came from.
We can just plug in some random numbers here
and then call it and let's use a smaller number here.
So just to that, the average Dolphin here
is actually more than double of the average Koalas.
So if you run this now,
then we see that the Dolphins win with 576 versus 111.
And so that's exactly the numbers
that we plugged in in here.
So once more, it's important to keep in mind
that these functions are all independent from another.
We just happen to call checkWiner here
with these values that we calculated before,
but it's not mandatory to do that,
any other values will work just as well.
And so we proved that here.
Anyway, now let's use test data two,
and actually what I'm gonna do
is to reassign these two scores here.
So we calculate them first,
then we call checkWinner
and then I'm going to recalculate them
and call checkWinner again.
What I mean by saying that
is that these need to be let variables
so that we can reassign them, remember that?
Const values cannot be mutated,
they cannot be changed,
and so they need to be let.
And then here, of course, we need to get rid of the let,
because we do not want to create new variables,
we just want to overwrite.
So we want to reassign
the values that we had earlier.
So this time the values
are 85, 54 and 41.
85, 54 and 41.
And I hope that's correct.
And then for the Koalas,
we have 23, 34 and 27.
So that's 23, 34 and 27.
Let's lock these actually also,
and then simply call checkWinner again,
and now with the new values
of score Dolphins and score Koalas that we just reassigned.
So let's see.
And now the Dolphins seven average score of 60
and to Koalas of 28.
And so now the Dolphins win
with a 60 versus 28.
So that's a close call.
So if the Koalas had three more points
then no one would win,
but the Dolphins managed to do it.
Great, I hope that this all made sense to you,
and I know it is probably still
a bit confusing at this point.
And if you did some things differently
well then once more data's completely okay.
It just means you're developing
your own coding style over time.
For example, here with the average scores,
of course you could have created new variables down here
for the test data too.
So that's just one of many things
that you could have done differently
which are perfectly fine.
As long as your results are the same, you're good to go.
So take some time and review this lecture.
Remember that you can check out my final code
also in the final folder and the GitHub repo,
and then you're ready to move on to our next big topic
in the section,
which is gonna be arrays.
So I hope to see you there soon.