[SOLVED] Paper Rock Scissors Assignment help requested sec 4


#1

Hi all,

Bit stuck at the moment. My prompt works, however input answer is not returned, thus else statement fires. Starting to go goggle eye and would really appreciate if someone could take a look at my code and offer some direction :slight_smile:

function computerPlay() {
let random = [“Rock”, “Paper”, “Scissors”]
let randomPick = random[Math.floor(Math.random() * random.length)]
//alert(randomPick);
if(randomPick === 0) {
return “Rock”
}
else if(randomPick === 1) {
return “Paper”
} else {
return “Scissors”
}
}

function singleRound(playerSelection, computerSelection) {
  if(playerSelection === "rock" && computerSelection === "paper") {
    alert("You Lose! Paper beats Rock")
  }
  else if(playerSelection === "paper" && computerSelection === "rock") {
    alert("You Win! Paper beats Rock")
  }
  else if(playerSelection === "paper" && computerSelection === "scissors") {
    alert("You Lose! Scissors beats Paper!")
  }
  else if(playerSelection === "scissors" && computerSelection === "paper") {
    alert("You Win! Scissors beats Paper")
  }
  else if(playerSelection === "scissors" && computerSelection === "rock") {
    alert("You Lose! Rock beats Scissors")
  }
  else if(playerSelection === "rock" && computerSelection === "scissors") {
    alert("You win! Rock beats Scissors")
  } 
  else if(playerSelection === "") {
    alert("Lets play another time")
  }
  else if(playerSelection === computerSelection) {
    alert("It's a tie!");
  } else {
    alert("Please input again");
  }
}
let playerSelection = prompt("Rock, Paper or Scissors?").toLowerCase()
const computerSelection = computerPlay()
singleRound(playerSelection, computerSelection)

#2

In your computer play function you return Rock, Paper or Scissors with an uppercase first letter. You don’t lowerCase it anywhere but in singleRound you only check against lowercase words.


#3

Thank you, corrected and all works fine!

One other issue; my code doesn’t seem to be making a random pick anymore with the return always being “scissors” - Any thoughts?


#4

When you run computerPlay you have the following lines.

let random = [“Rock”, “Paper”, “Scissors”]
let randomPick = random[Math.floor(Math.random() * random.length)]

The first line creates an array. The second line gets one of the entries in that array. Notice the random[ ]. The [] is how you access array elements. The Math.floor(Math.random() * random.length) returns a number that is either 0, 1 or 2. So random[0] or random[1] or random[2] is actually returning an array element. Not a number.

All you need to do after that in computerPlay is return randomPick. All those if else statements don’t return anything because randomPick isn’t a number. Therefore the function returns undefined.

Hope that helps.


#5

Brilliant, thank you loads! All works a treat now and i feel my grey matter starting to get a handle on all this. Cheers


#6

Not getting much luck with my loop working correctly:

let playerScore = 0
let computerScore = 0

function game() {
  for(i = 0; i < 5; i++) {
    singleRound()
    console.log("Player: " + playerScore + " Computer: " + computerScore)
  }
}
game()

The single game round was alerting the result but i changed to console.log and all works fine. Not entirely sure what is wrong with the for loop but it is printing all 5 loops in one go rather than a single count per game round?


#7

Where are you generating a player choice and computer choice? Nothing is being passed to singleRound in your loop but according to earlier code it took two arguments.


#8

I was using them as a counter. Wanted to loop thru singleRound 5x and count each score. But confused now how to loop this correctly…


#9

No, I mean where you are getting the player input and computer input?


#10

I thought the data was from singleRound?


#11

In your original code you had this, where you passed playerSelection and computerSelection to singleRound. You don’t need to be doing that in your updated code anywhere.


#12

Ahh right of course, the loop won’t work otherwise. Ok, so added prompt and moved the playerSelection and ComputerSelection into loop. Still having problems but its definitely getting there:

function game() {
let playerSelection = prompt(“Rock, Paper, Scissors?”)
let computerSelection = computerPlay()
for(i = 0; i < 5; i++) {
singleRound(playerSelection, computerSelection)
console.log("Player: " + playerScore + " Computer: " + computerScore)
}
}


#13

You would need to get a fresh playerSelection and computerSelection each round so it needs to be inside the loop.


#14

Thanks, the loop works. Struggling with adding the scores as the loop keeps printing “0”

function game() {

  for(let i = 0; i < 5; i++) {
    let playerSelection = prompt("Rock, Paper, or Scissors?")
    let computerSelection = computerPlay()
    
    console.log(playRound(playerSelection, computerSelection))
    console.log("Player: " + playerScore + " Computer: " + computerScore)
    
  }
}
game()

#15

Ignore last reply - Just seen I wasn’t adding the counter to the actual playRound function which completely makes sense now - Thanks for all the help :smile: