Why does my code add to playerScore every time it calls the playRound function, instead of adding to computerScore?
My project brief suggested calling the playRound function five times in the game function, since I haven't looked into how to "loop" the code to repeat the function calls.
My profile: https://www.theodinproject.com/lessons/foundations-rock-paper-scissors
I tried adding 1 to playerScore or computerScore (they are declared as global variables with value 0) when calling the playRound function.
I tried using the increment operator and I have tried using the addition assignment operator = 1
I thought the winning player's score would be increased by 1.
What actually happened: Each time the playRound function is called, playerScore is incremented by 1, which is inconsistent with the winner.
//write a program to play 'rock, paper, scissors' game against the computer //COMPUTER CHOICE- generate random choice of weapon let choice = ['rock', 'paper', 'scissors']; //select random array element from weapon array function getComputerChoice() { computerChoice = choice[(Math.floor(Math.random() * choice.length))]; return computerChoice; } //USER CHOICE- assign user choice from prompt input function getPlayerChoice() { playerChoice = prompt('Choose your weapon', 'rock, paper or scissors?'); return playerChoice; } //assign values to player variables const playerSelection = getPlayerChoice().toLowerCase(); const computerSelection = getComputerChoice(); //message to return to player let youWin = `You win, ${playerSelection} beats ${computerSelection}`; let youLose = `You lose, ${computerSelection} beats ${playerSelection}`; let youDraw = `It's a draw!`; //put message options into an array let message = [youWin, youLose, youDraw]; //make global player score variables let playerScore = 0; let computerScore = 0; //function to play one round function playRound() { if (playerSelection == computerSelection) { return youDraw; } else if (playerSelection == 'rock' && computerSelection == 'paper') { computerScore = computerScore++; return message[1]; //you lose } else if (playerSelection == 'rock' && computerSelection == 'scissors') { playerScore++; return message[0]; //you win } else if (playerSelection == 'paper' && computerSelection == 'rock') { playerScore++; return message[0]; //you win } else if (playerSelection == 'paper' && computerSelection == 'scissors') { computerScore++; return message[1]; //you lose } else if (playerSelection == 'scissors' && computerSelection == 'rock') { computerScore++; return message[1]; //you lose } else if (playerSelection == 'scissors' && computerSelection == 'paper') { playerScore++; return message[0]; //you win } else { return ('oops! Type rock, paper or scissors!') } } //function to play five rounds and report player as winner or loser at the end function game() { //check code: what values are assigned to player selections? console.log('player ', playerSelection); console.log('computer ', computerSelection); playRound(); playRound(); playRound(); playRound(); playRound(); return playRound(); } console.log(game()); console.log(computerScore); console.log(playerScore);
Some small changes will solve your problem. The main change is getting player choice and computer choice every round instead of just once. We will also generate youWin, youLose and other messages at the same time.