This is a game where the player plays against the computer in a rock paper scissors game. I am trying to make the computer score or player score go up depending on which one wins. But it does not increment when I try to run it. I am new to functions and returning values so I do not really understand it.
// Declaring variables let playerScore = 0; let computerScore = 0; // Gives a random value from the array function computerPlay(){ var things = ['rock', 'paper', 'scissors']; var random = things[Math.floor(Math.random()*things.length)]; console.log('The computer chose: ' + random); return random; } // plays a round of the game function playRound(playerSelection, computerSelection){ if(playerSelection === computerSelection){ console.log("tie"); } else if(playerSelection === "rock" && computerSelection === "paper"){ console.log("YOU LOSE"); computerWin(); } else if(playerSelection === "rock" && computerSelection === "scissors"){ console.log("YOU WIN"); playerWin(); } else if(playerSelection === "paper" && computerSelection === "rock"){ console.log("YOU WIN"); playerWin(); } else if(playerSelection === "paper" && computerSelection === "scissors"){ console.log("YOU LOSE"); computerWin(); } else if(playerSelection === "scissors" && computerSelection === "paper"){ console.log("YOU WIN"); playerWin(); } else{ console.log("YOU LOSE"); computerWin(); } } function playerWin(){ ++playerScore; console.log("Player Score is " + playerScore); } function computerWin(){ ++computerScore; console.log("Computer Score is " + computerScore) } // Call functions let chooseWord = "Choose "; let playerSelection = prompt(chooseWord); console.log(playerSelection.toLowerCase()); let computerSelection = computerPlay(); computerSelection.toLowerCase(); playRound(playerSelection, computerSelection); computerWin(); playerWin();
Advertisement
Answer
Overall code works well. The improvement to make it a game is to create a loop for a given score (here is to 3 wins).
// Declaring variables let playerScore = 0; let computerScore = 0; // Gives a random value from the array function computerPlay(){ var things = ['rock', 'paper', 'scissors']; var random = things[Math.floor(Math.random()*things.length)]; console.log('The computer chose: ' + random); return random; } // plays a round of the game function playRound(playerSelection, computerSelection){ if(playerSelection === computerSelection){ console.log("tie"); } else if(playerSelection === "rock" && computerSelection === "paper"){ console.log("YOU LOSE"); computerWin(); } else if(playerSelection === "rock" && computerSelection === "scissors"){ console.log("YOU WIN"); playerWin(); } else if(playerSelection === "paper" && computerSelection === "rock"){ console.log("YOU WIN"); playerWin(); } else if(playerSelection === "paper" && computerSelection === "scissors"){ console.log("YOU LOSE"); computerWin(); } else if(playerSelection === "scissors" && computerSelection === "paper"){ console.log("YOU WIN"); playerWin(); } else{ console.log("YOU LOSE"); computerWin(); } } function playerWin(){ ++playerScore; console.log("Player Score is " + playerScore); } function computerWin(){ ++computerScore; console.log("Computer Score is " + computerScore) } // Call functions const playToScore = 3 while(playerScore !== playToScore && computerScore !== playToScore) { let chooseWord = "Choose "; let playerSelection = prompt(chooseWord); console.log(playerSelection.toLowerCase()); let computerSelection = computerPlay(); computerSelection.toLowerCase(); playRound(playerSelection, computerSelection); console.log('Results are: Player -', playerScore, 'Computer -', computerScore) } if (playerScore === playToScore) { console.log('Player won with score', playerScore) } else if (computerScore === playToScore) { console.log('Computer won with score', computerScore) }