How to use a button together with a function? (JavaScript / HTML)

0

Hello, I'm writing a page with a Stone or Scissors game. And I would like the user to be able to choose between the three options through three buttons, which when clicked call a function that assigns a value to the variable relative to the user's move, and then makes comparisons to know who won (Computer or User). My problem is that I can not get the variable to receive the values equivalent to plays using the functions.

Note: My algorithm for comparing the plays of both the PC and the User is working, so I did not post it together.

var a = 0;

function Pedra() {
  return a = 1;
}

function Papel() {
  return a = 2;
}

function Tesoura() {
  return a = 2;
}

if (a == 1) {
  alert("pedra");
}
if (a == 2) {
  alert("Papel");
}
if (a == 3) {
  alert("tesoura");
}
<div>
  <div>
    <button type="button" onclick="Pedra()">Pedra</button>
  </div>

  <div id="papel">
    <button type="button" onclick="Papel()">Papel</button>
  </div>

  <div id="tesoura">
    <button type="button" onclick="Tesoura()">Tesoura</button>
  </div>
</div>
    
asked by anonymous 05.05.2018 / 01:35

3 answers

0

I do not know if I understood your question right, but come on.

1 - Methods in javascript follow the camelCase pattern, the first letter of the lowercase method being the first letter of the remaining uppercase words. Name the methods as being actions, in this case escolher (this did not influence your code not working, just good practice anyway)

2 - You do not need return in each method, since you only have to assign the value to the variable a that is declared globally

3 - For each click, it is necessary to call the verificarEscolha() method, so you will be able to actually display the selected value with each button click.

var a = 0;

function escolherPedra() {
  a = 1;
  verificarEscolha()
}

function escolherPapel() {
  a = 2;
  verificarEscolha()
}

function escolherTesoura() {
  a = 3;
  verificarEscolha()
}

function verificarEscolha(){
  if (a == 1) {
    alert("pedra");
  }
  if (a == 2) {
    alert("Papel");
  }
  if (a == 3) {
    alert("tesoura");
  }
}
<div>
  <div>
    <button type="button" onclick="escolherPedra()">Pedra</button>
  </div>

  <div id="papel">
    <button type="button" onclick="escolherPapel()">Papel</button>
  </div>

  <div id="tesoura">
    <button type="button" onclick="escolherTesoura()">Tesoura</button>
  </div>
</div>
    
05.05.2018 / 05:15
0

An even simpler way would be to pass the option that the user chose as a parameter of a function and there to do the verification

In this example I made with switch case , if you prefer you can keep if else

function jogar(usuario) {
  switch(usuario) { 
    case 1:
      alert("pedra");
      break;
    case 2:
      alert("Papel");
      break;
    case 3:
      alert("tesoura");
      break;
    default:
      alert("Opção inválida");
      break;
  }
}
<div>
  <div>
    <button type="button" onclick="jogar(1)">Pedra</button>
  </div>

  <div id="papel">
    <button type="button" onclick="jogar(2)">Papel</button>
  </div>

  <div id="tesoura">
    <button type="button" onclick="jogar(3)">Tesoura</button>
  </div>
</div>
    
05.05.2018 / 05:39
0

As I do not know how the rest of your code is, I'm going to assume that your only problem really is that you are not able to change the value of to .

Technically your code "is correct". So one possibility is that you're not really changing the same a variable that was declared at startup.

The only possibility for this is a scope problem:
If the a variable is not global , the variable a one, and the a variables are other, where each one is different / independent. So you'd actually be altering different variables.

To resolve this try declaring the a variable outside of any function.

Try to leave your code similar to this (it's working), and then change the testing to see where the error is:

 <script>

    function imprimeValorDoa(){
        alert("o valor de a é : " + a);
    }
    var a = 0;

    function Pedra() {
      a = 1;
    }

    function Papel() {
      a = 2;
    }

    function Tesoura() {
      a = 3;
    }

</script>

<div>
  <div>
    <button type="button" onclick="Pedra(); imprimeValorDoa()">Pedra</button>
  </div>

  <div id="papel">
    <button type="button" onclick="Papel(); imprimeValorDoa()">Papel</button>
  </div>

  <div id="tesoura">
    <button type="button" onclick="Tesoura(); imprimeValorDoa()">Tesoura</button>
  </div>
</div>
  

Note: What might be happening in your code is that actually   was yes changing the value of a correctly. However, the    if(a == ...) was executed before the button was clicked.
  This is because if is inside the script, and all scripts are   executed completely when the html page is loaded.

    
05.05.2018 / 05:36