-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
114 lines (101 loc) · 3.54 KB
/
main.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// ROCK PAPER SCISSOR
// created by AAQEEB HUSSAIN, ALBERT SZUPSZYNSKI AND COURTNEY SMITH
// Alert the user what the game is
alert("Welcome to ROCK,PAPER,SCISSORS");
let shouldWeStart = prompt("Should We Start Game? (type yes or no)");
let username = prompt(
"What is your name? (name should be no more then 10 characters and only contain letters)"
);
// check if username is valid
function checkUsername(name) {
if (!/^[a-zA-Z]+$/.test(name) || name.length > 10) {
username = prompt("Name not valid, please try again!");
checkUsername(username);
} else {
return name;
}
}
checkUsername(username);
let capitilzedUser = username[0].toUpperCase() + username.substring(1);
function startGame() {
console.log(capitilzedUser);
let userChoice = prompt("What is your choice? (rock, paper, scissors)");
let randomNumber = Math.floor(Math.random() * 100) + 1;
let computersChoice = assignCompChoice(randomNumber);
winner(userChoice, computersChoice);
if (userScore === 2 || computerScore === 2) {
alert(
`Thank You for playing! ${capitilzedUser} scored ${userScore} points, Computer scored ${computerScore} points`
);
userScore = 0;
computerScore = 0;
}
let playAgain = prompt("Do you want to play again (yes or no)");
if (playAgain.toLowerCase() === "yes") {
startGame();
} else {
alert(
`Thank You for playing! ${capitilzedUser} scored ${userScore} points, Computer scored ${computerScore} points`
);
}
}
// Write a variable with let computers choice
// Create a function that compares our choice and the computers choice
function assignCompChoice(randomNumber) {
if (randomNumber <= 33) {
return "rock";
} else if (randomNumber > 33 && randomNumber <= 66) {
return "paper";
} else {
return "scissors";
}
}
//function compering userChoice Vs computerChoice
let userScore = 0;
let computerScore = 0;
function winner(userPlayer, compPlayer) {
let userAnswer = userPlayer.toLowerCase();
if (userAnswer === compPlayer) {
alert(
`it is a draw ${capitilzedUser} score ${userScore} & ${computerScore}`
);
return;
} else if (
(userAnswer === "rock" && compPlayer === "scissors") ||
(userAnswer === "paper" && compPlayer === "rock") ||
(userAnswer === "scissors" && compPlayer === "paper")
) {
userScore++;
alert(` ${capitilzedUser} wins score ${userScore} & ${computerScore}`);
return;
} else {
computerScore++;
alert(` ${capitilzedUser} lost score ${userScore} & ${computerScore}`);
return;
}
}
//if (userPlayer === "rock" && compPlayer === "paper") {
// computerScore++;
// userScore--;
// return `userPlayer lost Your score ${userScore} & ${computerScore}`;
// } else if (userPlayer === "paper" && compPlayer === "scissors") {
// computerScore++;
// userScore--;
// return `userPlayer lost Your score ${userScore} & ${computerScore}`;
// } else if (userPlayer === "paper" && compPlayer === "rock") {
// userScore++;
// computerScore--;
// return `userPlayer wins Your score ${userScore} & ${computerScore}`;
// } else if (userPlayer === "scissors" && compPlayer === "paper") {
// userScore++;
// computerScore--;
// return `userPlayer wins Your score ${userScore} & ${computerScore}`;
// } else if (userPlayer === "scissors" && compPlayer === "rock") {
// computerScore++;
// userScore--;
// return `userPlayer lost Your score ${userScore} & ${computerScore}`;
// }
startGame();
//after deciding on the winner alert message + refresh website if you want to play again.
console.log(userChoice);
console.log(computersChoice);