0

i'm a novice in this world...so trying to begin, I started with an online tutorial. The exercise it's simple, but i can´t get the "empate" text on screen if the condition exists. Can you help me to know whats wrong?:

var usuarioElige = prompt("piedra, papel o tijera?");
var computadoraElige = Math.random();
if (computadoraElige <= 0.34) {
    computadoraElige = "piedra";
} else if(computadoraElige <= 0.67) {
    computadoraElige = "papel";
} else {
    computadoraElige = "tijera";
}

var comparar = function (usuarioElige,computadoraElige) {
    if (usuarioElige === computadoraElige) {
        return "¡Es un empate!";
    }
};
2
  • 1
    Because the entered number never matches the generated random number? Commented May 15, 2015 at 21:07
  • 1
    You are not calling comparar(). Commented May 15, 2015 at 21:09

2 Answers 2

1

You never call the function that prints out the "enpate" message. Try this version:

var usuarioElige = prompt("piedra, papel o tijera?");
var computadoraElige;

var d = Math.random();
if (d <=0.34){
    computadoraElige = "piedra";
}else if(d <=0.67){
    computadoraElige = "papel";
}else{
    computadoraElige = "tijera";
}

var comparar = function (x,y){
    if (x===y){
        alert("¡Es un empate!");
    }
};

comparar(usuarioElige, computadoraElige)

Note the added call to the function at the end and that I renamed the parameters of the comparar function to "x" and "y" to avoid confusion (it also works if you stay with the old name).

Another thing is that I put the random number for the computer in a separate variable. It can be confusing if the same variable means two different things depending on what part of the program you are on.

I also improved the whitespace indentation of your program. Programs are easier to understand if they are well indented :)

Sign up to request clarification or add additional context in comments.

3 Comments

it should be if (x===y){
Thanks!! it works. Now, what if i need to make another election in case of "empate"?
Use a while loop to repeat the election until you get a winner. The details I'll leave as an exercise :)
0

You're missing:

comparar(usuarioElige, computadoraElige);

At the end of the code

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.