Repository navigation
Expand file tree
/
Copy pathjavascript.js
More file actions
111 lines (107 loc) · 3.18 KB
/
Copy pathjavascript.js
File metadata and controls
111 lines (107 loc) · 3.18 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* Set some global variables to hold scores and how many games played */
let computerScore = 0;
let playerScore=0;
let tie=0;
let count=0;
/*
Gets a random number between 1 and 3 and assigns the text Rock, Paper, or Scissors the choice variable based on the number created.
*/
function getComputerChoice() {
let number = Math.floor((Math.random()* 3)+1);
let choice = "";
if (number === 1) {
choice = "Rock"
}
else if (number === 2) {
choice = "Paper"
}
else {
choice = "Scissors"
}
return choice;
}
/* Gets the player's choice and sets it to lower so they player can enter Rock, ROCK, or rock and it still works. Does not yet handle other erroneous input. */
function getPlayerChoice() {
let choice = prompt("Enter Rock, Paper, or Scissors.");
let lowerChoice = choice.toLowerCase();
return lowerChoice;
}
/* Main game logic. Gets both the computer and players choices and increments variables based on the results. */
function game () {
computer= getComputerChoice();
player= getPlayerChoice();
string= "";
console.log("The computer chooses: "+ computer)
if (computer=="Rock" && player=="rock") {
string = "It's a tie!";
tie+=1;
count+=1;
return string;
}
else if (computer=="Paper" && player=="paper") {
string = "It's a tie!";
tie+=1;
count+=1;
return string;
}
else if (computer=="Scissors" && player=="scissors") {
string = "It's a tie!";
tie+=1;
count+=1;
return string;
}
else if (computer=="Rock" && player=="paper") {
string = "You win! Paper covers rock!";
playerScore+=1;
count+=1;
return string;
}
else if (computer=="Rock" && player=="scissors") {
string = "You lose. Rock smashes scissors!";
computerScore+=1;
count+=1;
return string;
}
else if (computer=="Paper" && player=="rock") {
string = "You lose. Paper covers rock!";
computerScore+=1;
count+=1;
return string;
}
else if (computer=="Paper" && player=="scissors") {
string = "You win! Scissors cut paper!";
playerScore+=1;
count+=1;
return string;
}
else if (computer=="Scissors" && player=="rock") {
string = "You win! Rock smashes scissors!"
playerScore+=1;
count+=1;
return string;
}
else if (computer=="Scissors" && player=="paper") {
string = "You lose! Scissors cut paper";
computerScore+=1;
count+=1;
return string;
}
}
/* function for the score of the game using the variables in the main game logic */
function gamescore() {
if (count>= 5) {
console.log("Computer: "+ computerScore + " You: "+ playerScore + " Tied: "+tie+ " times");
}
}
/* Without looping, plays 5 games. After each game it displays the string that is returned from the main game logic notifying user of who won. Then calls the gamescore function to give results */
game();
console.log(string);
game();
console.log(string);
game();
console.log(string);
game();
console.log(string);
game();
console.log(string);
gamescore();