-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
94 lines (86 loc) · 3.07 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
function getComputerChoice() {
let choices = ["Rock", "Paper", "Scissors"];
let idx = Math.floor(Math.random() * choices.length);
return choices[idx];
}
function playRound(playerSelection, computerSelection, playerPoints, computerPoints) {
// your code here!
if (playerSelection.toLowerCase() === computerSelection.toLowerCase()) {
return [playerPoints+0, computerPoints+0, "Tied"];
} else if (
playerSelection.toLowerCase() === "rock" &&
computerSelection.toLowerCase() === "scissors"
) {
return [playerPoints+1, computerPoints+0, `You Win! ${playerSelection} beats ${computerSelection}!`];
} else if (
playerSelection.toLowerCase() === "scissors" &&
computerSelection.toLowerCase() === "paper"
) {
return [playerPoints+1, computerPoints+0, `You Win! ${playerSelection} beats ${computerSelection}!`];
} else if (
playerSelection.toLowerCase() === "paper" &&
computerSelection.toLowerCase() === "rock"
) {
return [playerPoints+1, computerPoints+0, `You Win! ${playerSelection} beats ${computerSelection}!`];
} else
return [playerPoints+0, computerPoints+1, `You Lose! ${computerSelection} beats ${playerSelection}!`];
}
function appendText(querySel, txt) {
const container = document.querySelector(querySel);
const content = document.createElement("p");
content.textContent = txt;
container.appendChild(content);
}
function replaceText(id, txt) {
document.getElementById(id).innerText = txt;
}
function displayMove(event, playerPoints, computerPoints) {
let playerChoice;
if (event.target.id === "rock") {
playerChoice = "Rock";
} else if (event.target.id === "paper") {
playerChoice = "Paper";
} else {
playerChoice = "Scissors";
}
const computerChoice = getComputerChoice();
const [playerScore, computerScore, winningText] = playRound(
playerChoice,
computerChoice,
playerPoints, computerPoints
);
// replaceText("playerChoice", playerChoice);
// replaceText("computerChoice", computerChoice);
appendText(".player", playerChoice);
appendText(".computer", computerChoice);
replaceText("result-text", winningText);
return [playerScore, computerScore];
}
function displayFinalScore(playerScore, computerScore) {
if (playerScore > computerScore) {
replaceText("gameResult", `Final: You Won ${playerScore} to ${computerScore}`);
} else if (computerScore > playerScore) {
replaceText("gameResult", `Final: You Lost ${playerScore} to ${computerScore}`);
} else {
replaceText("gameResult", `Final: You Tied ${playerScore} to ${computerScore}`);
}
document.getElementById('submit').style.visibility ='visible';
}
function game(numberGames) {
const buttons = document.querySelectorAll(".btn");
let count = 0;
let player = 0;
let computer = 0;
buttons.forEach(function (currentBtn) {
currentBtn.addEventListener("click", (event) => {
count++;
if (count <= numberGames) {
[player, computer] = displayMove(event, player, computer);
}
if (count == numberGames) {
displayFinalScore(player, computer);
}
});
});
}
game(5);