-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex-old.html
91 lines (90 loc) · 3.63 KB
/
index-old.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Rock Paper Scissors</title>
</head>
<body>
<script>
/*
computerPlay function randomly return Rock, Paper, or Scissors
Because We Have only three Choice I used Math.random() to randomly retrun from 1 to 3 number
and Math.floor() for convert the number to integer
*/
function computerPLay() {
let computerChoice = Math.floor(Math.random() * 3) + 1;
switch (computerChoice) {
case 1:
return "Rock";
break;
case 2:
return "Paper";
break;
case 3:
return "Scissors";
default:
return "Something Wrong in Computer Play Function";
break;
}
}
/*PlayRound function plays a single round the function takes two parameters and return
a string that declares the winner
the first parameter the player inputs it and the second parameter has a default value
and takes it from computerPlay function
*/
function playRound(playerSelection, computerSelection = computerPLay()) {
let playerChoice = playerSelection.toLowerCase();
let computerChoice = computerSelection.toLowerCase();
if (playerChoice === 'paper' && computerChoice === 'rock') {
return 1;
} else if(playerChoice === 'scissors' && computerChoice === 'paper'){
return 2;
} else if (playerChoice === 'rock' && computerChoice === 'scissors') {
return 3;
} else if(playerChoice === computerChoice){
return 4;
} else{
return `You Lose! ${computerChoice} beats ${playerChoice}`;
}
}
/*game function keeps the score and reports a winner or loser at the end
two variables for the two players scores (computer and player)
and loop for five times for the game rounds
each round adds one to the winner variable
and after the loop ends print the final result depending on the two variables
*/
function game() {
let playerScore = 0;
let computerScore = 0;
for (let i = 1; i <= 5; i++) {
let input = prompt('Please Choose: Rock, Paper or Scissors ');
let result = playRound(input);
if (result === 1) {
console.log('You Win! Paper beats Rock');
playerScore++;
} else if (result === 2) {
console.log('You Win! Scissors beats Paper');
playerScore++;
} else if (result === 3) {
console.log('You Win! Rock beats Scissors');
playerScore++;
} else if(result === 4){
console.log("It's a tie");
} else{
console.log(result);
computerScore++;
}
}
if (playerScore > computerScore) {
console.log(`Congrats You are a winner and the Result is: ${playerScore} : ${computerScore}`);
} else if(playerScore < computerScore){
console.log(`You are a Loser and the Result is: ${playerScore} : ${computerScore}`);
} else{
console.log("It's a tie after all");
}
}
game();
</script>
</body>
</html>