Rock, Paper, Scissors (The code just prints out the first round's result)


#1

Hi everybody. I can’t figure it out why my code doesn’t print out the result for each round and the winner. Any help?

   function computerPlay(){
   let options=["rock","paper","scissors"];
   return options[Math.floor(Math.random()*options.length)];
   }

   let playerSelection=prompt("Choose your option","");
   playerSelection=playerSelection.toLowerCase();
   let computerSelection=computerPlay();


   function playRound(playerSelection,computerSelection) {

	   if ((playerSelection==="rock" && computerSelection==="scissors") ||
	   (playerSelection==="scissors" && computerSelection==="paper") ||
	   (playerSelection==="paper" && computerSelection==="rock")) {

         return "You win";
       }


	   else if ((playerSelection==="scissors" && computerSelection==="rock") ||
	   (playerSelection==="paper" && computerSelection==="scissors") ||
	   (playerSelection==="rock" && computerSelection==="paper")) {

         return "Computer wins";
       }
   }

   function game() {
	   let computer;
	   let player;
	   for(i=1;i<=5;i++) {
		   result=playRound(playerSelection,computerSelection);
		   console.log(result);
		   if (result==="You win") {
			 player+=1;
			   }
		   else if(result==="Computer wins") {
			 computer+=1;
			   }
			 }
	   if (player>computer) {
	   console.log("You are the winner");
		 }
	
	   else if(player<computer) {
	   console.log("Computer is the winner");
		 }
   }

computerPlay();
game();

#2

Hey @krykck

There are a couple of issues here.

One is that you don’t initialise your computer and player scores so they default to undefined. And you can’t do undefined+=1.

Try let computer = 0 and let player = 0

Secondly, you don’t have any condition in your playRound function to check for a draw.

Thirdly, you generated the player and computer choices outside of the game loop, so they get set at the start and then cannot be changed. You should move:

   let playerSelection=prompt("Choose your option","");
   playerSelection=playerSelection.toLowerCase();
   let computerSelection=computerPlay();

to inside the game for loop right at the top above the result=playRound(playerSelection, computerSelection) line.

Hope that helps.


#3

Thank you for your help and quick reply! It works now. I was thinking that in the case of draw how can I change the code so that it plays until it finds a winner?

function computerPlay(){
   let options=["rock","paper","scissors"];
   return options[Math.floor(Math.random()*options.length)];
   }




   function playRound(playerSelection,computerSelection) {

	   if ((playerSelection==="rock" && computerSelection==="scissors") ||
	   (playerSelection==="scissors" && computerSelection==="paper") ||
	   (playerSelection==="paper" && computerSelection==="rock")) {

         return "You win";
       }


	   else if ((playerSelection==="scissors" && computerSelection==="rock") ||
	   (playerSelection==="paper" && computerSelection==="scissors") ||
	   (playerSelection==="rock" && computerSelection==="paper")) {

         return "Computer wins";
       }

       else if (playerSelection===computerSelection) {

         return "Draw";
       }
   }

   function game() {
	   let computer=0;
	   let player=0;
	   for(i=1;i<=5;i++) {
	   	   let playerSelection=prompt("Choose your option","");
		   playerSelection=playerSelection.toLowerCase();
		   let computerSelection=computerPlay();
		   result=playRound(playerSelection,computerSelection);
		   console.log(result);

		   if (result==="You win") {
			 player+=1;
			   }

		   else if(result==="Computer wins") {
			 computer+=1;
			   }
			 }

	   if (player>computer) {
	   console.log("You are the winner");
		 }

	   else if(player<computer) {
	   console.log("Computer is the winner");
		 }

	   else if(player===computer) {
	   console.log("Draw");
	   }
   }

computerPlay();
game();

#4

You would probably have to do a while loop that keeps going until either the computer or player score reached 5 instead of a for loop.