Help rock paper scissors game loop 5 times return wrong results

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

any help is appreciated, let me know if the first post describing the problem should be shorter or more straight to the point.

also let me know if you need more information to help, thanks.

Hi there,
You are declaring the variables (computerSelection) and (computerPlay) twice, once in the global scope and again inside of the game() loop, since your playRound() function can only get the (computerPlay) from the global scope it will never get the updated value from the loop.
In this case you just need to update the values without declaring it again inside of the game() loop:
Rock, Paper, Scissors fixed

1 Like

Thank you so much for helping me, now i understand what i was doing wrong all the time. :grin:
i spent a lot of time trying to make work the game but that time helped me to improve.

by the way i also saw that playerSelection was declared a second time inside the loop, even if the game worked that way i suppose that the code looks cleaner if i only declare it once, i tried it that way and the game keeps working.

i had to refine a bit the loop “if” win and lose conditions for the results to work better.

Exactly, in this case, this is exactly what you should do, because when you declare it a second time inside the loop, you are basically creating a new variable with the same name as the one in the global scope, the difference is that this variable will not be accessible outside the loop and will not share the same value, so you did right!

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

You are right, sorry about that.