Skip to content
Advertisement

Rock, Paper, Scissors game using Javascript

I’m attempting a Rock, Paper, Scissors game using Javascript. I’m new to Javascript, so I don’t know much. Each time I click a button I can get the values both for playerSelection and computerSelection but when I try to run the function playRound() it seems like it can’t “reach” the values returned by clicking the buttons. What am I doing wrong?

JavaScript
JavaScript
JavaScript

Advertisement

Answer

There are a couple issues here. First off, you are calling the playRound() function before any buttons are pressed. It’s called on load of the script and then never again. What you’d need to do is to call playRound() inside your click handler because that is the event which you need to test if the user won, lost, or tied.

Secondly, you are trying to return values from a click handler inside a .forEach, neither of which by definition return a value to their caller.

I think your best bet to solve this is to do a couple things:

  1. Move your computerPlay() into your click handler
  2. Move your playRound() into your click handler

Here’s an example of what it would look like:

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement