-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
79 lines (71 loc) · 2.3 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
let humanScore = 0;
let computerScore = 0;
let playCount = 0;
let results = ``;
let scoreText = ``;
playRound = (humanChoice, computerChoice) => {
if (humanChoice == computerChoice) {
return `Its a draw!`;
}
if (humanChoice == "rock" && computerChoice == "scissors") {
humanScore += 1;
return `${humanChoice.toUpperCase()} beats ${computerChoice.toUpperCase()}`;
}
if (humanChoice == "paper" && computerChoice == "rock") {
humanScore += 1;
return `${humanChoice.toUpperCase()} beats ${computerChoice.toUpperCase()}`;
}
if (humanChoice == "scissors" && computerChoice == "paper") {
humanScore += 1;
return `${humanChoice.toUpperCase()} beats ${computerChoice.toUpperCase()}`;
}
computerScore += 1;
return `${computerChoice.toUpperCase()} beats ${humanChoice.toUpperCase()}`;
};
getComputerChoice = () => {
let randomChoice = Math.random();
if (randomChoice >= 0 && randomChoice < 1 / 3) {
return "rock";
} else if (randomChoice >= 1 / 3 && randomChoice < 2 / 3) {
return "paper";
} else {
return "scissors";
}
};
getHumanChoice = () => {
let choice = prompt("Enter your choice (rock, paper or scissors): ");
choice = choice.toLowerCase();
if (choice == "rock" || choice == "paper" || choice == "scissors") {
return choice;
}
alert("Wrong Choice!!");
return getHumanChoice();
};
const buttons = document.querySelectorAll(".button");
const resultsDiv = document.getElementById("results");
const scoreDiv = document.getElementById("score");
const finalResults = document.getElementById("final-results");
scoreDiv.textContent = scoreText;
playGame = (choice) => {
results = ``;
finalResults.textContent = ``;
results = playRound(choice, getComputerChoice());
resultsDiv.textContent = results;
scoreText = `\nCurrent Score\nPlayer: ${humanScore}\nComputer: ${computerScore}\n`;
playCount += 1;
if (playCount > 5) {
if (humanScore > computerScore) {
finalResults.textContent = `You WIN!!`;
} else {
finalResults.textContent = `You LOST!!`;
}
playCount = 0;
humanScore = 0;
computerScore = 0;
}
scoreText = `\nCurrent Score\nPlayer: ${humanScore}\nComputer: ${computerScore}\n`;
scoreDiv.textContent = scoreText;
};
buttons.forEach((button) => {
button.addEventListener("click", () => playGame(button.id));
});