Rock paper scissors game code academy
Find centralized, trusted content and collaborate around the technologies you use most. Connect and share knowledge within a single location that is structured and easy to search. I can see that you have figured it out by now, but I gave it a try anyway out of curiosity. I made a few changes as you can see below. I had to rearrange some functions and do some nesting so that that the user would have a chance to make her choice before the result was calculated. I also removed some code to avoid text overlap.
I'll leave it to you to clean it up a little more, but your game now works correctly for every possible combination of user and computer choices. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 5 years, 5 months ago. Active 2 years, 11 months ago. Viewed times. Refresh to play again! Try again! Improve this question. Luka Jacobowitz Jordan Orchard Jordan Orchard 11 1 1 bronze badge. Next, you need to grab the elements using JavaScript and add an event listener to each one of them.
The querySelectorAll method is used because the returned NodeList object implements the forEach method. The callback function passed into addEventListener method will be executed when the user clicks on one of the three buttons. If the user clicked on "Rock" button, then pInput variable value will be of "Rock" string. The computer needs to select one of the three available choices. You can use the Math. When the inputs are not equal, you can start checking pInput value if it matches one of the three possible options.
The code below starts with checking if the user selects "Rock" for the current round:. After that, you need to check for both "Paper" and "Scissors" as follows:. With the function completed, you just need to call it inside the addEventListener method:.
With the core game mechanism set, you can start improving the game experience by keeping scores. For example, once the user or the computer gains five wins, the winner is called out and the score resets back to zero. This can be done by creating two variables that keep the scores for both the user and the computer. The variable values will be updated when the user or the computer wins, and another function will check if either variable is equal to five. Next, increment the pScore value by one when the user wins, or the cScore value when the computer wins:.
Once the scores are updated, call a new function to update the scores displayed in the browser. Finally, you need another function to check if one of the players already gained five wins. Call the checkWinner function right above the call to updateScore function. When it returns true , set the scores back to zero and call updateScore again to refresh the rendered scores:. Now your rock paper scissors game will keep the scores and declare the winner when 5 wins have been gained.
You can make the game interface better by using CSS to add colors and borders.
0コメント