repl.it
@KevinMulhern/

rock_paper_scissors

JavaScript

No description

fork
loading
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
function computerPlay() {
  return ["rock", "paper", "scissors"][randomIndex()];
}

function randomIndex() {
  return Math.floor(Math.random() * 3);
}

function playRound(playerSelection, computerSelection) {
  const winningChoice = winningMap()[computerSelection];
  playerSelection = playerSelection.toLowerCase();

  console.log(`The computer choose ${computerSelection}`);
  console.log(`You choose ${playerSelection}`);

  console.log(winningChoice)
  if (playerSelection === winningChoice) {
    return `You Win! ${playerSelection} beats ${computerSelection}\n`;
  } else if (playerSelection.toLowerCase() === computerSelection) {
    return `Tie! ${playerSelection} and ${computerSelection} are the same\n`;
  } 

  return `You Lose! ${computerSelection} beats ${playerSelection}\n`;
}

function winningMap(selection) {
  return { paper: "scissors", scissors: "rock", rock: "paper" };
}

function game() {
  for(let turn = 1; turn <= 5; turn++) {
    const playerSelection = window.prompt("rock, paper or scissors?:");
    const computerSelection = computerPlay();
    console.log(playRound(playerSelection, computerSelection));
  }
}

game()





Native Browser JavaScript
?