Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <title>Rock Paper Scissors Game</title>
- <head>
- <script>
- const getUserChoice = userInput => {
- userInput = userInput.toLowerCase();
- if(userInput === 'rock' || userInput === 'paper' || userInput === 'scissors' || userInput === 'bomb'){
- return userInput;
- }
- return 'error';
- }
- function getComputerChoice() {
- var rand = Math.floor(Math.random() * 4);
- var res = '';
- switch(rand) {
- case 1:
- res = 'rock';
- break;
- case 2:
- res = 'paper';
- break;
- case 3:
- res = 'scissors';
- break;
- default:
- res = "scissors";
- break;
- }
- return res;
- }
- function determineWinner(userChoice, computerChoice) {
- if(userChoice === computerChoice) {
- return 'Game was a tie.';
- }
- if(userChoice === 'rock') {
- if(computerChoice === 'paper') {
- return 'Computer won';
- } else {
- return 'user won';
- }
- }
- if(userChoice === 'paper') {
- if(computerChoice === 'scissors') {
- return 'computer won';
- } else {
- return 'user won';
- }
- }
- if(userChoice == 'scissors') {
- if(computerChoice === 'rock') {
- return 'computer won';
- } else {
- return 'user won';
- }
- }
- if(userChoice === 'bomb') {
- return 'user won';
- }
- }
- function playGame() {
- var userChoice = getUserChoice('scissors');
- var computerChoice = getComputerChoice();
- console.log("User: " + userChoice);
- console.log("Computer: " + computerChoice);
- console.log(determineWinner(userChoice, computerChoice));
- }
- playGame();
- </script>
- </head>
- <body>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement