-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
87 lines (80 loc) · 2.5 KB
/
Copy pathscript.js
File metadata and controls
87 lines (80 loc) · 2.5 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
78
79
80
81
82
83
84
85
86
87
// random(a,b) per generare un numero tra a e b
// creare una funzione per cui if a, b, c
// console.log rock paper o scissors
// function getComputerChoice
// return "rock" "paper" or "scissors" RANDOMLY
function getRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
function getComputerChoice() {
let randomElement = getRandomNumber(1, 3);
if (randomElement === 1) {
return "rock";
}
else if (randomElement === 2) {
return "paper";
}
else if (randomElement === 3) {
return "scissors";
}
}
let playerSelection;
playerSelection = playerSelection.toLowerCase();
let winner = "You won! ";
let loser = "You lost! ";
let playerScore = 0; // punteggio player
let computerScore = 0; // punteggio pc
function playRound(playerSelection, computerChoice) {
playerSelection = prompt("Rock, paper, scissors!");
if (playerSelection == computerChoice) {
playerScore += 0;
computerScore += 0;
return ("That's a tie!");
}
else if (playerSelection == "rock" && computerChoice == "paper") {
computerScore += 1;
return (loser + "Paper beats rock");
}
else if (playerSelection == "scissors" && computerChoice == "paper") {
playerScore += 1;
return (winner + "Scissors beats paper");
}
else if (playerSelection == "rock" && computerChoice == "scissors") {
playerScore += 1;
return (winner + "Rock beats scissors");
}
else if (playerSelection == "paper" && computerChoice == "scissors") {
computerScore += 1;
return (loser + "Scissors beats paper");
}
else if (playerSelection == "scissors" && computerChoice == "rock") {
computerScore += 1;
return (loser + "Rock beats scissors");
}
else if (playerSelection == "paper" && computerChoice == "rock") {
playerScore += 1;
return (winner + "Paper beats rock");
}
else {
return ("You spelt it wrong! Restart the game.");
}
}
// let result = playRound(playerSelection, computerChoice);
// console.log(result);
function game() {
for (let i = 0; i < 5; i++) {
let computerChoice = getComputerChoice();
let round = playRound(playerSelection, computerChoice);
console.log(round);
}
}
function scoreResult(){
game();
if (playerScore > computerScore) {
console.log("You won the game");
}
else if (computerScore > playerScore) {
console.log("You lost the game");
}
}
scoreResult();