Jonas: So 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 calc average function,
as in 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 the 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 calc average and then let's just use some random numbers
because here we know that
this is supposed to be four, right?
So we have 3, 4, 5.
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 calc average.
And now we passed in the 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 in the values 44, 23, and 71.
Then for the koalas, it works the same way.
So score, koalas equals calc average,
and now, we have 65, 54,
and 49.
So 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.
So 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 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 coat.
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 these 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 now actually implement that functionality.
So that's the function check winner.
And so, it takes the average dolphins
and average koalas parameters, okay?
And then it locks the winner to the console.
So let's do that.
And once 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 we 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 right?
That's the condition.
So again, a team only wins if it has
at least double 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 doubled the amount of point 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 log to the console.
Dolphins win the trophy.
So 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.
So for example, 30 versus 13.
So both points with the versus in between.
So that's average dolphins versus average koalas.
So you see that here, we are also practicing all
the other skills that we already acquired before,
especially in the previous section, right?
Okay. And now here, we gonna just need to do the opposite.
So else if and now
if the koalas have 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 let's just grab this one from here,
koalas,
average koalas,
and then average dolphins,
average, yes.
All right, 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 number 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.
Okay.
And now, all we need to do is to call this function here.
So check winner, and we will do that
with score dolphins
and score koalas.
Okay? So let's recap.
We calculated the score
of the dolphins up here and of the koalas as well.
And now, we call the check winner function here, okay?
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.
Now, all right?
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.
Okay? 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 check winner 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 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 these values came from.
We can just plug in some random numbers here
and then call it, and let's use a smaller number here.
Okay, so just so 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, okay?
So once more, it's important to keep in mind
that these functions are all independent from another.
We just happen to call check winner 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 prove that here.
Anyway, now, let's use test data 2.
And actually what I'm gonna do is
to reassign these two scores here.
So we calculate them first, then we call check winner,
and then I'm going to recalculate them
and call check winner again.
So what I mean by saying that is that these need
to be let variables, so that we can reassign them.
Remember that cons 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
that 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 40 koalas.
We have 23, 34, and 27.
So that's 23, 34, and 27.
Let's lock these actually also.
And then simply call check winner again.
And now, with the new values
of score dolphins and score koalas,
they'll be just reassigned, okay?
So let's see.
And now, the dolphins have an average score
of 60 and the koalas of 28.
And so now, the dolphins win with 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. So 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, that is 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 2.
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 are 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, in the GitHub repo,
and then you're ready to move on to our next big topic
in the section, which is gonna be a race.
So I hope to see you there soon.