Skip to content

Commit

Permalink
completed basic rock paper scissors console game in js!
Browse files Browse the repository at this point in the history
  • Loading branch information
khantzawphyo committed Apr 29, 2024
1 parent 557bd11 commit 9d00001
Showing 1 changed file with 62 additions and 4 deletions.
66 changes: 62 additions & 4 deletions js/script.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,65 @@
choices = ["rock", "paper", "scissors"];
const getComputerChoice = () => choices[Math.floor(Math.random() * choices.length)];

const getComputerChoice = () => {
choices = ["Rock", "Paper", "Scissors"];
return choices[Math.floor(Math.random() * choices.length)];
function getHumanChoice() {
const userChoice = prompt("Enter your choice (rock, paper, scissors): ").toLowerCase();
if (userChoice === "rock" || userChoice === "paper" || userChoice === "scissors") {
return userChoice;
}
else {
alert("Invalid choice!");
return getHumanChoice();
}
}

console.log(getComputerChoice());
function playGame() {
let humanScore = 0;
let computerScore = 0;


function playRound(humanChoice, computerChoice) {
humanChoice = humanChoice.toLowerCase();

let result;
let explanation;

if (humanChoice === computerChoice) {
result = "It's a tie!";
explanation = "";
}
else if (
(humanChoice === "rock" && computerChoice === "scissors") ||
(humanChoice === "paper" && computerChoice === "rock") ||
(humanChoice === "scissors" && computerChoice === "paper")
) {
result = "You Win!";
explanation = `${humanChoice} beats ${computerChoice}`;
humanScore++;
}
else {
result = "You Lose!";
explanation = `${computerChoice} beats ${humanChoice}`;
computerScore++;
}

console.log(`${result} ${explanation}`);
}

for (let i = 0; i < 5; i++) {
const humanChoice = getHumanChoice();
const computerChoice = getComputerChoice();
playRound(humanChoice, computerChoice);
}

if (humanScore > computerScore) {
console.log("Congratulations! You win the game!");
} else if (humanScore < computerScore) {
console.log("Sorry, you lose the game. Better luck next time!");
} else {
console.log("It's a tie! The game ends in a draw.");
}


}

playGame();

0 comments on commit 9d00001

Please sign in to comment.