-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.js
More file actions
81 lines (61 loc) · 2.44 KB
/
Copy pathjavascript.js
File metadata and controls
81 lines (61 loc) · 2.44 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
function getComputerSelection(){
let choices = ['Rock', 'Paper', 'Scissors']
// gets the computer selection
// this is the main branch
let randomNumber;
randomNumber = Math.floor(Math.random() * 3);
return choices[randomNumber];
}
function playRound(playerSelection, computerSelection){
// returns 0 if the round is a tie
// returns 1 if the user wins
// returns 2 if the computer wins
playerSelection = playerSelection.toLowerCase()
computerSelection = computerSelection.toLowerCase()
if (playerSelection == computerSelection){
return 0
} else if ((playerSelection == 'rock' && computerSelection == 'scissors')
|| (playerSelection == 'scissors' && computerSelection == 'paper')
|| (playerSelection == 'paper' && computerSelection == 'rock')) {
return 1
} else return 2;
}
const playButton = document.getElementById("play-button");
const buttons = document.querySelectorAll('.options button');
let playerScore = 0;
let computerScore = 0;
// Add a click event listener to each button
buttons.forEach(button => {
button.addEventListener('click', () => {
let computerSelection = getComputerSelection();
const option = button.dataset.option;
let roundResult = playRound(option, computerSelection)
if (roundResult === 1){
playerScore++;
document.getElementById("gameScore").innerHTML = "You won!"
document.getElementById("userScore").innerHTML = playerScore;
} else if (roundResult === 2){
computerScore++;
document.getElementById("gameScore").innerHTML = "You lost!"
document.getElementById("computerScore").innerHTML = computerScore;
} else {
document.getElementById("gameScore").innerHTML = "Tie!"
}
console.log(`Computer: ${computerSelection}`)
console.log(`Player Selection: ${option}`)
console.log(`Round result: ${roundResult}`)
});
});
playButton.addEventListener("click", function(e){
e.target.parentNode.classList.add('opacity')
e.target.style.opacity = 0;
const header = document.querySelector('.header');
// header.classList.add('opacity')
// header.style.opacity = 0;
const playSection = document.getElementById("play-section");
const scores = document.getElementById("scores")
setTimeout(function() {
playSection.classList.remove('hidden')
scores.classList.remove('hidden')
}, 1000);
});