Hi, im new to javascript, i was following other javascript course and i stumbled with making a rock paper scissors game using the java console, i did right the code for a single playround, but then the task required to make a loop so the game plays for five rounds, thats where i could not be able to do it and after getting a bit frustrated i jumped on freecodecamp javascript course.
I did the basic javascript course, everything went fine, and i even polished a bit the single round rock paper scissors everytime i tried to fix the game, short the if statements adding “or” and also added a playscore and a computerscore, i finally was able to make the game loop for 5 rounds but i dont understand why the return value win, lose or tie within the loop is not working correctly.
for testing purposes i leave the game run once and the loop 4 times so i get 5 playrounds.
more than the solution i would like to know what im doing wrong so i can learn.
heres the code in codepen so you can run the game and see the problem.
rock paper scissors game
function playRound(playerSelection, computerSelection) {
if (playerSelection.toLowerCase()=="rock" && computerSelection[computerPlay]=="scissors" || playerSelection.toLowerCase()=="paper" && computerSelection[computerPlay]=="rock" || playerSelection.toLowerCase()=="scissors" && computerSelection[computerPlay]=="paper"){
playerScore++;
return win;
}
else if (playerSelection.toLowerCase()=="rock" && computerSelection[computerPlay]=="paper" || playerSelection.toLowerCase()=="scissors" && computerSelection[computerPlay]=="rock" || playerSelection.toLowerCase()=="paper" && computerSelection[computerPlay]=="scissors"){
computerScore++;
return lose;
}
else if (playerSelection.toLowerCase() == computerSelection[computerPlay]){
tieScore++;
return tie;
} //if player input rock compare to rock, its a tie
else {
return "you must input rock, paper or scissors";
} //else if player input paper compare to rock, you win
}
let playerSelection = prompt("lets play rock papper scissors"); //define player, player input
//let player = playerSelection.toLowerCase(); //convert user input to lowercase *old
console.log ("you choose " +playerSelection.toLowerCase()); //show user input in lowercase
let computerSelection = ["rock","paper","scissors"];
let computerPlay= Math.floor(Math.random() * computerSelection.length);
console.log("The computer choose " +computerPlay, computerSelection[computerPlay]);//define computer input
//define scores
let playerScore=0;
let computerScore=0;
let tieScore=0;
//define win, lose or tie
const win="You Win this round!\n -------";//define win
const lose="You Lose this round!\n -------";//define lose
const tie= "This round is a tie!\n -------";//define tie
//define playround
console.log(playRound(playerSelection, computerSelection));
//loop
function game() {
for (let i = 0; i <4; i++) { //what is going to be loop
let playerSelection = prompt("lets play rock papper scissors"); //define player, player input
console.log ("you choose " +playerSelection.toLowerCase()); //show user input in lowercase
let computerSelection = ["rock","paper","scissors"];
let computerPlay= Math.floor(Math.random() * computerSelection.length);
console.log("The computer choose " +computerPlay, computerSelection[computerPlay]);//define computer input
console.log(playRound(playerSelection, computerSelection));
}
if (playerScore>computerScore){
return "You Win this game.\n -------";
}
if (playerScore==computerScore){
return "This game is a tie.\n -------";
}
else{
return "You Lose this game.\n -------";
}
}
console.log(game()); //play the loop
console.log( "Your final score is " +playerScore +" wins " +computerScore +" loses and " +tieScore +" draws.\n -------");
let score=playerScore+computerScore+tieScore;
console.log("You played this game " +score +" times.\n -------\nGame over.");
//count win lose or tie