Rock Paper Scissor Questions

Hi All,

I just started the Odin Project about a few weeks ago and I’m stuck on a few parts.

  1. console.log(game()) is only returning “Computer score: 1” and not the player score even when “You WIn…” is returned from playRound().

  2. I am trying to keep the prompt box open for 5 tries but I cannot find a way to do that. How can I run this “function” 5 times when it is set to variable playerSelection?

  3. I tried running a for loop to playRound() 5 times within the game() function but I have no idea how to make it run 5 times while asking for user input through prompt. It keeps returning a score of 1.

Thank you very much!

function computerPlay() {

let x = Math.floor(Math.random() * 3)

if (x == 0) {
    return 'rock'
} else if (x == 1) {
    return 'paper'
} else {
    return 'scissor'
}

}

//function playerPlay() {

// let x = Math.floor(Math.random() * 3)

// if (x == 0) {
// return ‘rock’
// } else if (x == 1) {
// return ‘paper’
// } else {
// return ‘scissor’
// }
//}

let playerSelection = prompt(‘Rock, Paper, or Scissor?’).toLowerCase()

let x = 0;
let y = 0;

function playRound(playerSelection, computerSelection) {

if (playerSelection == 'rock' && computerSelection == 'paper') {
    return "You Lose! Paper beats Rock";
    
} else if (playerSelection == 'paper' && computerSelection == 'rock') {
return "You Win! Paper beats Rock";
    
} else if (playerSelection == 'rock' && computerSelection == 'scissor') {
return "You Win! Rock beats Scissor";
    
} else if (playerSelection == 'scissor' && computerSelection == 'rock') {
return "You Lose! Rock beats Scissor";
    
} else if (playerSelection == 'paper' && computerSelection == 'scissor') {
return "You Lose! Scissor beats Paper";
    
} else if (playerSelection == 'scissor' && computerSelection == 'paper') {
return "You Win! Scissors beats Paper";
    
} else if (playerSelection == computerSelection) {
    return 'draw'
}

}

const computerSelection = computerPlay()
console.log('You chose: ’ + playerSelection, ';Computer chose: ’ + computerSelection)
console.log(playRound(playerSelection, computerSelection))

function game() {
for (i=0; i<5; i++) {
playRound()
}

if (playRound() == "You Lose! Paper beats Rock" || "You Lose! Rock beats Scissor" || "You Lose! Scissor beats Paper") {
    
    x++
    return 'Computer score: ' + x

} else if (playRound() == "You Win! Paper beats Rock" || "You Win! Rock beats Scissor" || "You Win! Scissor beats Paper") {
    
    y++
    return 'Player Score: ' + y

}  else if (playRound() == 'draw') {
    
    x += 0
    return
}

}

console.log(game())

  1. On if (playRound() == "You Lose! Paper beats Rock" || "You Lose! Rock beats Scissor" || "You Lose! Scissor beats Paper") you need to repeat playRound == 'some string' on every or ( || )
    You can’t use a == 1 || 5 || 10 to check if a is any of those values. What the expression actually means is (a == 1) || (5) || (10). Even if a is let’s say 3, the second term, 5, by itself will evaluate to boolean true. In your case, the first if is always executed because “You Lose! Rock beats Scissor” is true.
    By the way, you’re calling playRound without parameters, basically doing playRound(null, null) so it’s returning ‘draw’.

  2. Move let playerSelection = prompt(‘Rock, Paper, or Scissor?’) inside the for loop. The user will be asked for input every time the for repeats.

  3. In a for loop:

    1. ask for user input
    2. generate computer input
    3. play a round
    4. output a message (console.log) depending on the result of the round
1 Like

Thank you so much!

Your comment about #3 really tied everything together. I ended up creating the loop below and it works pretty well.

for (x==0 && y == x; x<3 && y<3;) {
let playerSelection = prompt(‘Rock, Paper, or Scissor?’).toLowerCase()
const computerSelection = computerPlay()
playRound(playerSelection, computerSelection)
console.log(playRound(playerSelection, computerSelection))
console.log('You chose: ’ + playerSelection, ';Computer chose: ’ + computerSelection)
console.log(game(playerSelection, computerSelection))

}

I’m glad that I could help.

I think you wanted to set x and y to 0 in the loop initialization for (x = 0 , y = x; x<3 && y<3;) not check whether they are equal to 0; but, other than that, good job!