repl.it
@macloo/

RockPaperScissors

ES6

Starter for a script with a lot of pseudo code.

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
// 2 players
// 3 possible choices for each player
const choices = ['rock', 'paper', 'scissors'];

// get rock, paper or scissors randomly
function makeChoice(choices) {
  let rand = Math.floor(Math.random() * 3);
  return choices[rand];
}

// Randomly get a choice for player 1 - rock or paper or scissors
let player1 = makeChoice(choices);
// Randomly get a choice for player 2 - rock or paper or scissors
let player2 = makeChoice(choices);

// test it
console.log("Player 1 has " + player1);
console.log("Player 2 has " + player2);

// Compare the 2 choices and figure out who won
// Or is it a tie?
// test for a tie FIRST because it is easy 
if (player1 === player2) {
  console.log("It's a tie!");
}

// rock and paper - paper wins
// rock and scissors - rock wins
// paper and rock - paper wins
// paper and scissors - scissors wins
// scissors and rock - rock wins
// scissors and paper - scissors wins

// Tell the result - write “You won!” Or “The computer won!”
// Ask if they want to play again
// Keep score? You won 2 times, and the computer won 5 times 
Babel Compiler v6.4.4 Copyright (c) 2014-2015 Sebastian McKenzie