Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="utf-8"/>
- <title>Console Based Rock-Paper-Scissors</title>
- <style>
- </style>
- </head>
- <body>
- </body>
- <script>
- //Player Moves
- let playerHand = prompt('Pick your destiny');
- playerHand = normalize(playerHand);
- play(playerHand);
- function normalize(playerHand) {
- let selectionLower = playerHand;
- let selectionUpper = playerHand;
- selectionLower = selectionLower.slice(1, playerHand.length)
- selectionLower = selectionLower.toLowerCase();
- selectionUpper = selectionUpper.charAt(0);
- selectionUpper = selectionUpper.toUpperCase();
- return selectionUpper + selectionLower;
- }
- function play(playerHand) {
- if (playerHand === 'Rock') {
- let playerSelection = 'Rock';
- console.log('You threw ' + playerSelection + '!');
- } else if (playerHand === 'Paper'){
- let playerSelection = 'Paper';
- console.log('You threw ' + playerSelection + '!');
- } else if (playerHand === 'Scissors'){
- let playerSelection = 'Scissors';
- console.log('You threw ' + playerSelection + '!');
- } else {
- alert('Please enter your move, Rock, Paper, or Scissors')
- let playerHand = prompt('Pick your destiny');
- playerHand = normalize(playerHand);
- play(playerHand);
- }
- }
- //Computer Moves
- let hands = ['Rock', 'Paper', 'Scissors'];
- computerPlay(hands);
- function computerPlay(array) {
- let hand = array[random(array.length, 0)];
- return hand;
- }
- function random(upper, lower) {
- let num = Math.floor(Math.random() * (upper - lower) + lower);
- return num
- }
- const computerSelection = computerPlay()
- console.log(playRound(playerSelection, computerSelection))
- </script>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement