odin-rock-paper-scissors/script.js

82 lines
2 KiB
JavaScript
Raw Normal View History

2023-09-30 10:23:21 -06:00
function getComputerChoice(){
let compAnswer = Math.floor(Math.random() * 3) + 1;
if (compAnswer === 1) {
return "rock";
} else if (compAnswer === 2) {
return "paper";
} else {
return "scissors"
}
2023-09-30 10:23:21 -06:00
}
function playRound(playerSelection, computerSelection){
p = playerSelection.toLowerCase();
c = computerSelection.toLowerCase();
2023-09-30 10:23:21 -06:00
2023-09-30 10:52:40 -06:00
if (p === c) {
return "tie";
2023-09-30 10:52:40 -06:00
} else if (p === "rock") {
if (c === "paper") {
return "player loses";
} else { //scissors
2023-09-30 10:52:40 -06:00
return "player wins";
}
2023-09-30 10:52:40 -06:00
} else if (p === "paper") {
if (c === "scissors") {
return "player loses";
} else { //rock
2023-09-30 10:52:40 -06:00
return "player wins";
}
2023-09-30 10:52:40 -06:00
} else if (p === "scissors") {
if (c === "paper") {
return "player loses";
} else { //scissors
return "player wins";
}
} else {
return "not a valid entry";
}
2023-09-30 10:23:21 -06:00
}
function game() {
let p = 0;
let c = 0;
for (var i = 0; i < 5; i++) {
const ps = prompt("Rock, Paper or Scissors?");
const cs = getComputerChoice();
play = playRound(ps, cs);
switch (play) {
case "player loses":
c += 1;
console.log("You lost!");
break;
case "player wins":
p += 1;
console.log("You won!");
break;
case "tie":
p += 1;
c += 1;
console.log("It's a tie!");
break;
default:
console.log("Invalid selection. Please choose Rock, Paper or Scissors to play.");
}
}
console.log("Computer won " + c + " games");
console.log("Player won " + p + " games");
2023-09-30 10:23:21 -06:00
//playRound(playerSelection, computerSelection)
//count how many times p and c have won
//return whether or not p has won
}
console.log(game());