-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.js
63 lines (59 loc) · 2 KB
/
logic.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
let runningScore = 0;
const computerPlay = () => {
let dice = Math.floor(Math.random() * 3) + 1;
switch(dice) {
case 1:
return "rock";
break;
case 2:
return "scissors";
break;
case 3:
return "paper";
break;
default:
console.log("dice broken.");
return;
}
};
const playOneRound = (playerChoice, computerChoice) => {
switch(playerChoice) {
case "rock":
if(computerChoice === "scissors") {
runningScore++;
scoreVal.textContent = runningScore;
return "You win! Rock beats scissors.";
}
else if(computerChoice === "paper") return "You lose. Paper beats rock.";
else return "Tie! Both chose rock.";
break;
case "paper":
if(computerChoice === "rock") {
runningScore++;
scoreVal.textContent = runningScore;
return "You win! Paper beats rock.";
}
else if (computerChoice === "scissors") return "You lose. Scissors beat paper";
else return "Tie! Both chose paper.";
break;
case "scissors":
if(computerChoice === "paper") {
runningScore++;
scoreVal.textContent = runningScore;
return "You win! Scissors beat paper.";
}
else if(computerChoice === "rock") return "You lose. Rock beats scissors.";
else return "Tie! Both chose scissors."
break;
default: return "Sorry, invalid choice...";
};
};
const btns = [...document.querySelectorAll('.content button')];
const scoreVal = document.querySelector('#score-value');
const result = document.querySelector('p.result');
btns.map(b => {
b.addEventListener('click', e => {
let roundResult = playOneRound(e.target.value, computerPlay());
result.textContent = roundResult;
});
});