In the previous lesson, we showed you how we can set up the player selection and track that information,
and then earlier we also saw how we can generate a random selection for the computer play.
Now we need to compare both of those results and determine who wins.
So setting up a variable, we're going to call it result, and we're going to link that to a function
that we're going to create, which will be check winner.
We need to pass in some values into it.
So one of the values that we need to pass in is the player selection.
So let's set up a variable for that.
So player selection, and that's going to equal to what we were originally outputting into the console.
And we saw that earlier as rock.
And then we also saw that the computer selection is there as well.
So taking the player selection and it actually doesn't matter which one is first, it will differentiate
between the logic.
So we've got player selection and computer selection.
We need to pass both of those values into our result function in order to be able to check those values
and determine a winner.
And that means that we need to create a function for this.
So creating a function, we're going to call it check winner.
And within this function, we've got two parameters.
So we've got the player and we've got the computer and we need to do a comparison.
And this is going to be quite a few different conditions that we're checking.
So we're checking to see if the player is equal to the computer selection.
And if it is, then we're going to simply return back a draw, the return back, the word draw.
And I'm going to also add in one more line here where we're going to log out the results of result.
So that was the first condition.
So that takes care of if both of them are matching the next one.
Let's check to see if the player has selected rock, if the player has selected rock, that we know
that the computer will lose if the computer has selected scissors.
But we know that the computer will win if he is selected paper.
So we're going to check to see if the computer has selected paper and we're going to do all of this,
all of the logic here by wind conditions.
So if the computer selected paper, the player selected Rock, we're going to return back the computer.
So that's going to be the winner.
And we're was going to return back the string of whatever the winner is.
And if it's not a computer, then we're going to return back the player.
And the reason here is that the player is going to be the winner in this scenario.
So unless the computer has returned back, rocked the only other choice, because we've already taken
the condition checking for a draw.
So that means that they're not both the same.
The player selected rock, and the only other choice is that the computer selected, if it didn't select
Rock, if it didn't select paper, then it selected scissors.
And that means that the player one condition.
So this one is going to be very similar.
And this is the other alternative.
If a player selected paper, we're going to check to see what the computer selected.
So the same format as just above.
We're going to check to see if the computer selected scissors and if the computer selected scissors,
then that means that the computer wins.
And if the computer didn't select scissors, there's only one last option.
And that means that the player won because the computer selected rock.
So refresh.
Refresh it and we'll hit rock, so we selected Rock, the computer selected paper, and that means that
the computer won because the paper beats the rock.
Well, try rock one more time.
We see we both selected rock, so it returned back to draw, and this time we selected Rock Computer,
selected scissors.
So that means that we were the winner.
So that was a nice selection of all the different options.
And we could try the same thing for paper again, paper draw.
It's always a good idea to play through it a little bit just to make sure that your logic is working,
it's being applied properly and that you are returning back the correct winner.
Remember again that the order here is important if you switch it around in this logic isn't going to
work and you have to switch these values around as well, or you can switch around some of the logic.
So go ahead and add this into your project, try it out and make sure that you are returning the correct
winner and you can be ready to move on to the next step where we're going to be associating score and
outputting who the winner is to the player.
So that is all still yet to come.