-: [Jonas Schmedtmann] This is the first part of the solution
for the coding challenge that I just
gave you in the last lecture.
So let's start by creating a new file
which has all of this code so that
we don't mess up our original code.
So I select and copy all of this
and now I'm just creating a new file here.
This one will be called challenges.js.
So I paste all my code here and
I will put these challenges at the top.
Don't need this one here too.
And we already know the game rules.
Alright, so here are all three challenges.
Actually, another thing is that now I have
to comment out this part of the code
and link our new javascript file
to this html document.
So script, source, and now challenges.
So let's start with the first one, of course.
So a player loses his entire score
when he rolls two six in a row.
So as I already mentioned in the hint,
we need a variable where we can store previous dice rolls.
So let's do that.
And of course, we are going to do that
right here in the function which handles
all of our dice number generation
and displaying in the user interface.
So our dice value is generated here.
What I'm now going to do is to create
a variable at the very end of this function
but still inside of this if statement
that we added in the last lecture.
So here I'm going to declare a new variable.
Var,
lastDice,
And what I'm going to do is to set it to dice.
So let me explain that.
So we generate a dice number here,
then we do all of this stuff,
and then we check the dice number.
So if we then store the last dice number
we can use that variable the next time
that the function runs.
But actually not yet,
because this variable
should not be declared here,
but in a global scope.
Let me simply create a new var here: lastDice.
And the reason for that is that once the function returns,
so this function here returns,
then the value of lastdice will get lost
if we only declare it in here.
So for example, the value stored
in this dice variable here will not be accessible to us
after this function returns.
But we need this lastDice value the next time
that someone hits the roll button.
And that's because now,
down here in our if else statement
we are going to add another condition.
So we want the player to lose his score
when the current value and the
lastDice value are both a six.
So how do we do that?
So, dice must be six and now
the boolean operators are back,
so the dice value should be six
and the lastDice should also be six.
And then, player loses score and then
of course, we still also want this part here.
So else, and then the rest of the code that we already had.
OK, so once more, at the end of this function
we stored the value that we rolled
with our dice in an external variable.
So a variable that was defined outside
of the scope of this function here.
So we do that at the end of all this code here,
so that the next time that someone rolls the dice
we have still access to that variable
and can then perform this check here.
When we just rolled a six,
and we rolled a six the last time,
then the player loses his score.
And if this does not happen,
then we, of course, apply the logic that we had before.
Alright, so how does the player lose the score?
Remember, we used a scores variable
which is an array to store these values.
So all we need to do is to send it back to zero.
And such as have done all the time,
we use the activePlayer variable here
to define which score it is from
the first or the second player and set it back to zero.
So that's really simple.
And then, of course, we need to update
the dump as well, so that on our webpage
we see this value, zero, as well.
So instead of writing it all over again,
make it a little bit quicker,
I will just get it from down here.
So, now it's not scores,
well we could actually leave this as it is here
because scores activePlayer is now zero.
But we can also just set it to zero, right?
Because we already know that that is the value.
And what also happens then,
is that it's now next player's turn.
And so, therefore, we once again use our
nextPlayer function that we created before
to apply the don't repeat yourself or dry principle.
And now this should work.
So let's come back here and refresh this.
So let's try.
Actually I should hold because
otherwise there's no score to lose, right?
Alright, so we see we lost some score here,
we didn't see it because it was too quick.
So let me try it again.
Alright, so here we had it.
Once again we lost a score here
after rolling two six in a row.
It's not really visible in the video
but I actually clicked twice and there
were two sixes and we lost the score.
And I'm sure you will experience that for yourself as well.
So in the next video, we have the solution
for the second challenge.