-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
121 lines (108 loc) Β· 3.05 KB
/
script.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
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
112
113
114
115
116
117
118
119
120
121
//Storing Dom elements as variables
let computerChoice = document.getElementById("computerChoice");
let results = document.getElementById("resultDisplay");
let playerChoice = document.getElementById("playerChoice");
let playerScore = document.getElementById("playerScore");
let computerScore = document.getElementById("computerScore");
let paper = document.getElementById("paper");
let rock = document.getElementById("rock");
let scissors = document.getElementById("scissors");
//initializing variables for player's choice
let playerVal;
let computerVal;
let playerScores = 0;
let computerScores = 0;
//Adding events to my buttons to store values
paper.addEventListener("click", () => {
playerVal= "paper";
playGame();
});
rock.addEventListener("click", () => {
playerVal = "rock";
playGame();
});
scissors.addEventListener("click", () => {
playerVal = "scissors";
playGame();
});
//Letting the computer choose.
let compChoice = () => {
let choices = ["rock", "paper", "scissors"];
let randomIndex = Math.floor(Math.random() * choices.length);
computerVal = choices[randomIndex];
return computerVal;
}
//Helper function for findWinner. Needs to be refactored.
function compareValues(val1, val2) {
if (val1 === "rock") {
if (val2 === "scissors") {
return 1;
} else {
return -1;
}
}
if (val1 === "scissors") {
if (val2 === "paper") {
return 1;
} else {
return -1;
}
}
if (val1 === "paper") {
if (val2 === "rock") {
return 1;
} else {
return -1;
}
}
}
//This function helps find a tie, a win or a lose.
let findWinner = (str1, str2) => {
if (str1 === str2) {
return 0;
} else {
return compareValues(str1, str2);
}
};
//Display message of wins, losses and ties
function displayMessage(val1, val2) {
let res = findWinner(val1, val2);
if (res === 0) {
results.textContent = "It's a tie !!!";
} else if (res === 1) {
results.textContent = "Congrats!! You won this round."
playerScores ++;
playerScore.textContent = `Player two score: ${playerScores}`;
} else {
results.textContent = "Sorry. You lost this round."
computerScores++;
computerScore.textContent = `Player one score: ${computerScores}`;
}
}
//Display emojis in the player's div container
function displayPlayerEmojis() {
if (playerVal === "paper") {
playerChoice.textContent = "π€π½";
}else if (playerVal === "scissors") {
playerChoice.textContent = "βπ½";
}else if (playerVal === "rock") {
playerChoice.textContent = "βπ½";
}
}
//Display emojis in the computer's div container
function displayComputerEmojis() {
if (computerVal === "rock") {
computerChoice.textContent = "βπ½";
}else if (computerVal === "paper") {
computerChoice.textContent = "π€π½";
}else if (computerVal === "scissors") {
computerChoice.textContent = "βπ½";
}
}
//Function to play the game
function playGame() {
compChoice();
displayMessage(playerVal, computerVal);
displayPlayerEmojis();
displayComputerEmojis();
}