-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
77 lines (69 loc) · 2.13 KB
/
main.js
File metadata and controls
77 lines (69 loc) · 2.13 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
/// PROJECT CODE-NAME: 'RockPaperScissors' ///
// USER CHOICE
var getUserChoice = function (userInput) {
userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors' || userInput === 'bomb') {
return userInput;
} else {
return "Invalid response! Please pick: 'rock', 'paper', or 'scissors'.";
}
}
// COMPUTER CHOICE
var getComputerChoice = function () {
var randomNumber = Math.floor(Math.random() * 3);
if (randomNumber === 0 ) {
return "rock";
} else if (randomNumber === 1) {
return "paper";
} else {
return "scissors";
}
}
// DETERMINE WINNER
var determineWinner = function (userChoice, computerChoice) {
if (userChoice === computerChoice){
return "tie game";
} if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return "The computer won!";
} else {
return "The user won!";
}
} if (userChoice === 'paper') {
if (computerChoice === 'scissors') {
return "The computer won!";
} else if (computerChoice === 'rock') {
return "The user won!";
}
} if (userChoice === 'scissors') {
if (computerChoice === 'rock') {
return "The computer won!"
} else if (computerChoice === 'paper') {
return "The user won!"
}
}
}
// PLAY GAME
function playGame () {
var userChoice = getUserChoice('rock');
console.log("User choice: " + userChoice);
if (userChoice === "Invalid response! Please pick: 'rock', 'paper', or 'scissors'.") {
return; // Stop execution to prevent 'undefined'
}
var computerChoice = getComputerChoice();
console.log("Computer choice: " + computerChoice);
var cheatResult = cheatCode(userChoice);
if (cheatResult) {
return cheatResult;
} else {
return determineWinner (userChoice, computerChoice);
}
}
// CHEAT CODE
var cheatCode = function(userChoice) {
if (userChoice === 'bomb') {
return "BOOM! The user wins!";
}
}
// START GAME
playGame();