At the infant stages of learning JS. My RPS project works for the most part... The problem I cannot get around is that the game is calling for a 'human' input and computer selection once, then runs this selection 5x instead of calling for a new selection for each round. I've beat my head on this for several days to no avail.
let person = prompt(
'What will it be? Rock, Paper, or Scissors.'
);
person = person.toUpperCase();
console.log(person); // show human selection
let computer = [
Math.floor(Math.random() * 3) + 1
];
let roundCount = 0;
let humanWins = 0;
let compWins = 0;
if (computer == 1) {
computer = 'ROCK';
} else if (computer == 2) {
computer = 'PAPER';
} else if (computer == 3) {
computer = 'SCISSORS';
} else {
computer = 'No true statements';
}
console.log(computer); // show comp selection
function playGame(peep, comp) {
roundCount++;
if (peep === comp) {
console.log('Tie', +roundCount);
return 'Tie';
} else if (
(peep === 'ROCK' &&
comp === 'SCISSORS') ||
(peep === 'PAPER' && comp === 'ROCK') ||
(peep === 'SCISSORS' && comp === 'PAPER')
) {
humanWins++;
console.log(
'Human wins this round',
+roundCount,
+humanWins
);
return 'Human wins this round';
} else {
compWins++;
console.log(
'Skynet wins this round',
+roundCount,
+compWins
);
return 'Skynet wins this round';
}
}
function game() {
for (let i = 0; i <= 5; i++)
if (humanWins === 5 || compWins === 5) {
console.log('game over');
} else {
playGame(person, computer);
}
return humanWins, compWins;
}
game();
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src='script.js'></script>
<script>
</script>
</body>
</html>
I don't understand why it takes a single round and runs that result 5 times.