loading
index.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
45
46
47
48
49
50
51
52
var RPS = {
    opts: ['rock', 'paper', 'scissor'],
    choose: function() {
        var choice = Math.floor(Math.random() * 3)
        return this.opts[choice]
    },
    decoder: function(x){
        switch(x){
        case "rock": return 0; break;
        case 'paper': return 1; break;
        case "scissor": return 2; break;
        default:
          console.log("Incorrect entry!") 
          return 3
        }
    },
    win: function(x, y) {
      X = this.decoder(x)
      Y = this.decoder(y)
      if ((X == 0 && Y == 2) || (X == 1 && Y == 0) || (X == 2 && Y == 1)){
        return true
      }else{
        return false
      }
    },
    play: function(times) {
      userwin = 0
      aiwin = 0
      for(var i=0; i < times; i++){
        x = prompt("Enter rock, paper, or scissor")
        console.log("User choice: " + x)
        y = this.choose();
        console.log("AI choice: " + y)

        if (this.win(x, y)){
          userwin ++;
          console.log("<User wins>")
        } else if (this.win(y, x)){
          aiwin ++;
          console.log("<AI wins>")
        }else {console.log("<Tie!>")}
    }
    if (aiwin > userwin){
      console.log("AI won with "  + userwins + ' - ' + aiwins)
    }else if (userwin > aiwin){
      console.log("User won with " + userwins + ' - ' + aiwins)
    }else
      console.log("Both parties tied at: " + userwins + ' - ' + aiwins)
  }
};

RPS.play(10)
Fetching token