Jonas: So here goes my solution
to coding challenge number two.
So let's start by calculating the average scores.
So var scoreJohn is equal to, and we have the points,
so 89 plus 120 plus 103 divided by three.
Then var scoreMike is equal to 116 plus 94
plus 123 divided by three again.
Now let's simply log these to the console.
scoreJohn and scoreMike and here we go.
So the average score for John 104, and for Mike 111.
So Mike is clearly the winner here,
and so let's now log that to the console.
Okay, and so what we're gonna do here
is of course an if else statement.
So give it some space here, and now if scoreJohn
is greater than scoreMike, well then log to the console
that John's, now we need to escape this quote here,
so John's team wins with, and now let's also
include the score here as I mentioned
in the instructions, so scoreJohn points.
Alright, and else if, if it's the
other way around basically, so scoreMike
is greater than scoreJohn, well then
let's of course log that Mike's team wins.
Then here we also need to use the score of Mike, okay?
And then else, and that's the situation
where there is a draw, so if John's score
is not greater than Mike's and Mike's score
is also not greater than John, well then it means
that they have the same score, okay?
And so that is here in this else block,
and so in here we can simply log that there is a draw.
So let's test it out, and so yeah,
Mike's team wins with 111 points.
Let's now increase this here, let's say to 129,
and so now probably John wins.
And yeah, here we go, so now John's team wins.
Now let's try to have a tie here.
So we had 89 before, let's put it to 95, and we need
some more points here so lets increase this to 115.
That was a bit too much, 112 maybe.
And now we're almost there I guess.
Yeah, so now they have the same scores, so both have 111,
and so now we see that there is a draw,
and so of course none of these situations here apply,
because John's score is not greater than Mike's score,
and Mike's score is not greater than John's score,
and so of course we have a draw.
So that is the easier part.
Now let's also add Mary to the game.
So var scoreMary equals, and let's get her points.
So 97, 134, and 105.
This should be a comma, obviously, and now,
this should be plus signs of course, sorry for that.
So we add all of them together and then divide it by three.
Also log it to the console just to make sure.
So she has 112, let's put back what we had initially,
so it was 89, and so now we have these
three different scores, alright?
So let's get rid of this part, and I'm not gonna delete it,
I'll just comment it out, and okay.
So how can John win the game?
Basically he has to have more points than Mike
and also more points than Mary, right?
And so that's what we have to translate into code now.
So an if else statement, and we have to say that scoreJohn
should be greater than scoreMike and scoreJohn
should be greater than scoreMary.
Okay and that's it.
So does that make sense?
That is a great use case of the and operator here.
And it works like this because we need this here to be
true and at the same time we need this to be true.
So John's score needs to be greater
than both Mary's and Mike's, alright?
And so only in that case we will want
to print that John's team wins the game.
And then from here, it's actually pretty straightforward.
So once we understand how the logic works,
all we have to do is to do the same for the other players.
So how does Mike win the game?
Well of course he has to have a larger score
than the other two, so scoreMike needs to be greater
than scoreJohn and at the same time,
scoreMike, and what happened here,
scoreMike greater than scoreMary.
So John, and here we have Mike, and of course scoreMike.
And then finally the same for Mary.
So scoreMary needs to be larger than scoreJohn,
and scoreMary greater than scoreMike.
And now we say that Mary's team wins and here the scoreMary.
Alright, now what about the other situations?
Well let's just use an else here
and say that there is a draw, okay?
So this doesn't really cover all of the different
situations here, but that's not a big deal.
So for this small example, it's okay.
Imagine that Mary and John had both the same score,
but a larger score than Mike, so they two would have a tie,
but would still have more points than Mike,
and so that's one of the situations in which we will enter
this else block and display that there is a draw, okay?
So these three situations here only work for a clear winner,
alright, but again that's not a big deal.
We don't need to cover every single situation here.
So these are the more important of course,
where we can define which one is the clear winner.
So let's try it out now, and indeed
Mary's team wins with 112 points.
Let's add some points here to Mike,
and if we add three points, then he will have
the same score as Mary, so let's put it to 19 here,
and they now have both 112, and so we see
that there is a draw, so perfect.
And if we now increase this even further,
let's say 29, well then of course Mike's team
has the most points, and now finally let's add
some more points here to John, just to test
that all three cases work, and indeed,
now he has the most points and he is declared the winner.
Great, so that's it.
I hope you figured it out and so now you are
really ready to finally move on to the next part
of this intro section, which is the hugely important topic
of functions, so join me right there in the next video.