-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
completed basic rock paper scissors console game in js!
- Loading branch information
1 parent
557bd11
commit 9d00001
Showing
1 changed file
with
62 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |