-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
36 lines (29 loc) · 1.05 KB
/
script.js
File metadata and controls
36 lines (29 loc) · 1.05 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
const prompt = require("prompt-sync")();
let wins = 0;
let losses = 0;
let ties = 0;
while (true) {
const playerChoice = prompt("Enter rock, paper of scissors (to quit, enter 'quit'): ");
if (playerChoice.toLowerCase() === "quit") {
break;
}
if (playerChoice !== "rock" && playerChoice !== "paper" && playerChoice !== "scissors") {
console.log("Please enter a valid choice.");
continue;
}
const choices = ["rock", "paper", "scissors"];
const randomIndex = Math.round(Math.random() * 2);
const computerChoice = choices[randomIndex];
console.log("The computer chose:", computerChoice);
if (computerChoice === playerChoice) {
console.log("Draw match!");
ties++;
} else if ((playerChoice === "paper" && computerChoice === "rock") || (playerChoice === "rock" && computerChoice === "scissors") || (playerChoice === "scissors" && computerChoice === "paper")) {
console.log("Player Won!");
wins++;
} else {
console.log("Player Lost!");
losses++;
}
}
console.log("Wins:", wins, "Losses:", losses, "Ties:", ties);