forked from itscodenation/flw1-rps-15-16-starter
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathscript.js
More file actions
executable file
·67 lines (55 loc) · 1.61 KB
/
script.js
File metadata and controls
executable file
·67 lines (55 loc) · 1.61 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
var userPoint = 0;
var aiPoint = 0;
// This function returns the selection of the computer
function getAISelection() {
var random = Math.random();
if (random < 1/3) {
return 'rock';
}
if (random < 2/3) {
return 'paper';
}
return 'scissors';
}
// This function picks the winner
function pickWinner(userValue, aiValue) {
//TODO: pick the correct winner: user or ai
//TODO: Add one point for the winner
if (userValue === aiValue) {
return 'draw';
}
if (
userValue ==='rock' && aiValue === 'scissors' ||
userValue ==='paper' && aiValue === 'rock' ||
userValue ==='scissors' && aiValue === 'paper'
) {
userPoint++;
return 'user';
}
aiPoint++;
return 'ai';
}
// This function sets the scoreboard with the correct points
function setScore() {
$('#userPoint').text(userPoint);
$('#aiPoint').text(aiPoint);
}
// This function captures the click and picks the winner
function evaluate(evt) {
var userValue = evt.target.getAttribute('id');
var aiValue = getAISelection();
var winner = pickWinner(userValue, aiValue);
setScore();
if ( 'user' === winner ) {
$('#message').delay(50).text('You have won!, Click a box to play again');
} else if ( winner === 'draw' ) {
$('#message').delay(50).text('Draw! Click a box to play again');
} else {
$('#message').delay(50).text('You have lost!, Click a box to play again');
}
}
// This function runs on page load
$(document).ready(function(){
setScore()
$('.token').on('click', evaluate);
});