I've made a simple rock paper scissors game using javascript. Any feedback appreciated. This is a first project I've done with almost no help
let computerScore = 0;
let humanScore = 0;
function getComputerChoice() {
let computerChoice = Math.floor((Math.random() * 100)/40); //generates a number between 0 and 2
if (computerChoice === 0) {
computerChoice = 'rock';
} else if (computerChoice === 1) {
computerChoice = 'paper';
} else {
computerChoice = 'scissors';
}
return computerChoice;
}
function getHumanChoice() {
let humanChoice = prompt('Choose rock, paper, or scissors:');
if (humanChoice === null) return null; // if player clicks cancel on the prompt stop the function
humanChoice = humanChoice.toLowerCase(); // makes case insensitive
while (humanChoice !== 'rock' && humanChoice !== 'paper' && humanChoice !== 'scissors') {
alert('Please enter a valid choice');
humanChoice = prompt('Choose rock, paper, or scissors:');
}
return humanChoice;
}
function playRound(humanChoice, computerChoice) {
let result;
if (humanChoice === computerChoice) {
result = 'it\'s a tie.';
} else if (humanChoice === 'rock' && computerChoice === 'scissors' || humanChoice === 'paper' && computerChoice === 'rock' || humanChoice === 'scissors' && computerChoice === 'paper') {
result = 'you win.';
humanScore++;
} else {
result = 'you loose.'
computerScore++;
}
console.log(` You chose ${humanChoice} computer chose ${computerChoice} ${result}`)
console.log(` Your score: ${humanScore}\n Computer score: ${computerScore}`);
}
function playGame() {
// calls playRound function until someone gets the score of 3
while (computerScore < 3 && humanScore < 3) {
const humanTurn = getHumanChoice();
const computerTurn = getComputerChoice();
if (humanTurn === null) { // if prompt was canceled function stops
console.log("Game exited.");
return;
}
playRound(humanTurn, computerTurn);
}
if (computerScore < humanScore) {
alert('You win!');
} else if (computerScore > humanScore) {
alert('Computer wins :\'(');
} else {
alert('It\'s a tie.');
}
}
playGame();