Rock, Paper, Scissor!


#1

Hey guys! I am having trouble with my function game()…

I don’t know how to iterate my loop to add each score. I’m really stuck right here! This is what I have so far…any advice would be greatly appreciated!

var rock; var scissors; var paper; function computerPlay(){ const myArray = ['rock', 'paper', 'scissors']; return myArray[Math.floor(Math.random() * myArray.length)]; } function playRound (playerSelection, computerSelection){ if (playerSelection == computerSelection){ return 'It is a tie!'; }else if (playerSelection == 'rock' && computerSelection == 'scissors'){return'You win!' } else if (playerSelection == 'paper' && computerSelection == 'rock'){return 'You win!' } else if (playerSelection == 'scissors' && computerSelection == 'paper'){return'You win!' } else return 'Computer wins!'; } function game(){ for (let i = 0; i < 5; i++){ let computerScore = 0; let playerScore = 0; let computerSelection = computerPlay(); let playerSelection = prompt('pick your weapon'); let roundResult = playRound(playerSelection, computerSelection); console.log(roundResult); }

#3
 function game(){ 
for (let i = 0; i < 5; i++){ 
let computerScore = 0; 
let playerScore = 0;
let computerSelection = computerPlay(); 
let playerSelection = prompt('pick your weapon');
let roundResult = playRound(playerSelection, computerSelection);
console.log(roundResult); 
}

This is your for loop for function game() broken down line for line for visibility.

First thing that stands out is I do not think there is a closing bracket for the function, but you did close the for loop.

Second, remember each time that loop iterates you are essentially resetting your game. What I
mean by that is when i=0 i<5 you set computerscore to 0 playerscore to 0 etc etc… now once the for loop runs and goes back through at 1 now. so i=1 1<5 so we enter the loop. and you set computerscore to 0 again playerscore to 0 etc etc.

I do not think a for loop is beneficial to use in this scenario for your code or for the game in general to be honest. You should definitely try using a global variable of computerscore and playerscore and increment them using conditional statements e.g. if computer wins in your other function then in addition to returning “You win” you also increment score by 1 for computer and a counter by 1. :smiley:


#4

@smarcus4 Thanks alot for the advice! I changed my code like you said and put playerScore and computerScore into the original playRound function that I wrote and it made much more sense.

I was looking at some of the other examples for the rock, paper, scissor game and noticed alot of people used the while loop. I was unfamiliar with that, so i did some research on it and noticed my game looks and works much better now!

I also changed the game around so it would never end in a draw, first to 3 points wins!


#6

@jmast02 No problem dude! I believe I also used a while loop, definitely a valuable tool in your coding arsenal! Right on, sounds like a working application is finished to me :sunglasses:

a for loop is usually better when you want a piece of code to run a certain number of times, and a while loop is better when the condition for the code to keep running is more general