-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
87 lines (73 loc) · 2.78 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
function getRandomComputerResult() {
const options = ["Rock", "Paper", "Scissors"];
const randomIndex = Math.floor(Math.random() * options.length);
return options[randomIndex];
}
//console.log(getRandomComputerResult());
function hasPlayerWonTheRound(player, computer) {
return (
(player === "Rock" && computer === "Scissors") ||
(player === "Scissors" && computer === "Paper") ||
(player === "Paper" && computer === "Rock")
);
}
//console.log(hasPlayerWonTheRound("Paper", "Scissors"));
//console.log(hasPlayerWonTheRound("Scissors", "Rock"));
let playerScore = 0;
let computerScore = 0;
function getRoundResults(userOption) {
const computerResult = getRandomComputerResult();
if (hasPlayerWonTheRound(userOption, computerResult)) {
playerScore++;
return `Player wins! ${userOption} beats ${computerResult}`;
} else if (computerResult === userOption) {
return `It's a tie! Both chose ${userOption}`;
} else {
computerScore++;
return `Computer wins! ${computerResult} beats ${userOption}`;
}
}
//console.log(getRoundResults("Paper"));
//console.log("Player Score: ", playerScore, "Computer Score: ", computerScore);
const playerScoreSpanElement = document.getElementById("player-score");
const computerScoreSpanElement = document.getElementById("computer-score");
const roundResultsMsg = document.getElementById("results-msg");
const winnerMsgElement = document.getElementById("winner-msg");
const optionsContainer = document.querySelector(".options-container");
const resetGameBtn = document.getElementById("reset-game-btn");
function showResults(userOption) {
roundResultsMsg.innerText = getRoundResults(userOption);
computerScoreSpanElement.innerText = computerScore;
playerScoreSpanElement.innerText = playerScore;
if (playerScore === 3 || computerScore === 3) {
winnerMsgElement.innerText = `${
playerScore === 3 ? "Player" : "Computer"
} has won the game!`;
resetGameBtn.style.display = "block";
optionsContainer.style.display = "none";
}
};
// showResults("Rock");
function resetGame() {
playerScore = 0;
computerScore = 0;
computerScoreSpanElement.innerText = computerScore;
playerScoreSpanElement.innerText = playerScore;
roundResultsMsg.innerText = "";
winnerMsgElement.innerText = "";
optionsContainer.style.display = "block";
resetGameBtn.style.display = "none";
};
resetGameBtn.addEventListener("click", resetGame);
const rockBtn = document.getElementById("rock-btn");
const paperBtn = document.getElementById("paper-btn");
const scissorsBtn = document.getElementById("scissors-btn");
rockBtn.addEventListener("click", function () {
showResults("Rock");
});
paperBtn.addEventListener("click", function () {
showResults("Paper");
});
scissorsBtn.addEventListener("click", function () {
showResults("Scissors");
});