Same answer every time in rock, paper and scissors

Hello everyone! I try the “Rock, paper and scissors” challenge by my own. I change the form CodeAcademy tell you to do it, but i have some problems. Maybe you can help me.

I writed the code in Spanish, rock = piedra, paper = papel and scissors = tijeras

  1. When the Player use any option, the computer will give everytime the same option. If i choose “Papel” the computer everytime will chose “Tijeras”.

  2. When the Player choose “Tijeras”, computer every time choose “Tijeras” so is a tie. I F5 a lot of times but the answer of the computer don’t change.

User prompt!

//Prompt donde el usuario elige su opcion
    //Se guarda en la variable eleccionJugador
    var eleccionJugador = prompt("¿Papel, piedra o tijeras?");

    //Con ! estas diciendo que si no ingresan nada (es falsol) ejecute {}
    // ! operador que pasa de verdadero a falso y viceversa
    if (! eleccionJugador) {
        document.write("<p>Intentalo denuevo, presiona F5.</p>")
    } 
    /* Si el bloque de arriba es falso, ejecuta este. Si el jugador ingreso txt
    ejecuta esto */

    else {
        document.write("<p>Jugador:" + "" + eleccionJugador + "</p>");
    }

Computer random selection

/Eleccion aleatoria del ordenador
    var OrdenadorMates = function getRandomInt(minimo, maximo) {
        return Math.floor(Math.random() * (maximo - minimo )) + minimo;
    }

    var Ordenador = OrdenadorMates(1, 4); {
        if (Ordenador === 1) {
            Ordenador = "Piedra";
        } 
        else if (Ordenador === 2) {
            Ordenador = "Papel";
        }
        else (Ordenador === 3) {
            Ordenador = "Tijeras";
        }
        
    }

    document.write("<p>Ordenador:" + "" + Ordenador + "</p>");


Function comparison

 //Funcion de comparaciones

    var Comparacion = function(eleccion1, eleccion2) {
        if (eleccion1 === eleccion2) {
            return "Empate";
        }
        if (eleccion1 === "Piedra") {
            if (eleccion2 === "Tijeras") {
                return "Ganaste!";
            } else {
                return "Perdiste!";
            }
        }
        if (eleccion1 === "Tijeras") {
            if (eleccion2 === "Papel") {
                return "Ganaste!";
            } else {
                return "Perdiste!"
            }
        }
        if (eleccion1 === "Papel") {
            if (eleccion2 === "Piedra") {
                return "Ganaste!"; 
            } else {
                return "Perdiste!";
            }
        }
    };

    //Usar funcion Comparacion y guardar resultado.
    var Resultados = Comparacion(eleccionJugador, Ordenador);

    //Mostrar resultado
    document.write("<br><hr><br>" + Resultados);

Thank you all, love this community!

If the same player always wins, then you’d look at the condition that determines the winner.
What input are you giving to the condition? What output do you get from the condition? Was the input correct? Was the output correct for that input?

Look at the individual operations that you are carrying out, not just the final result.

To be honest it is quite hard to read your code when it is in Spanish… But the syntax is universal so on to it.

On the prompt part, you may want to build in a fail-safe that checks if you typed the words correctly. Right now the code expects your choices written with capitals but you do not ask for it. Also, you don’t tell the user he has provided an incorrect input.

¿Papel, piedra o tijeras?

Then on to the computer selection:

First thing I notice, else statement can’t have a condition. Remove the condition and you should be good to go.

On top of that, it may be your personal taste but I have learned that in Javascript you write your variable and function names in lower case on the first part.

so

var computerChoice
//instead of
var ComputerChoice 

Hope this helped,

Happy coding!

Thank you for open my eyes, I introduce all the possibilities. It seems like that:

var comparacion = function (eleccion1, eleccion2) {
    if (eleccion1 === eleccion2) {
        return "Empate!";
    }
    if (eleccion1 === "Piedra") {
        if (eleccion2 === "Tijeras") {
            return "Ganaste!"
        } else {
            return "Perdiste!"
        }
    }
    if (eleccion1 === "Piedra") {
        if (eleccion2 === "Papel") {
            return "Perdiste!"
        } else {
            return "Ganaste!"
        }
    }
    if (eleccion1 === "Tijeras") {
        if (eleccion2 === "Piedras") {
            return "Perdiste!"
        } else {
            return "Ganaste!"
        }
    }
    if (eleccion1 === "Tijeras") {
        if (eleccion2 === "Papel") {
            return "Ganaste!"
        } else {
            return "Perdiste!"
        }
    }    
    if (eleccion1 === "Papel") {
        if (eleccion2 === "Piedra") {
            return "Ganaste!"
        } else {
            return "Perdiste!"
        }
    }  
    if (eleccion1 === "Papel") {
        if (eleccion2 === "Tijeras") {
            return "Perdiste!"
        } else {
            return "Ganaste!"
        }
    } 
};    

Thank you to much!

Thank you to much for help, i fix the else error, noob erro probably. The code seems like that. Already fixed it.

But i have another question, as you say prompt only recognize if the first letter of your option is upper. How i can get all types of cases as correct?

var eleccionJugador = prompt("¿Papel, piedra o tijeras?");

//Con ! estas diciendo que si no ingresan nada (es falsol) ejecute {}
// ! operador que pasa de verdadero a falso y viceversa
if (! eleccionJugador) {
    document.write("<p>Intentalo denuevo, presiona F5.</p>")
} 
/* Si el bloque de arriba es falso, ejecuta este. Si el jugador ingreso txt
ejecuta esto */

else {
    document.write("<p>Jugador:" + "" + eleccionJugador + "</p>");
}
//Eleccion aleatoria del ordenador
var OrdenadorMates = function getRandomInt(minimo, maximo) {
    return Math.floor(Math.random() * (maximo - minimo )) + minimo;
}

var Ordenador = OrdenadorMates(1, 4); {
    if (Ordenador === 1) {
        Ordenador = "Piedra";
    } 
    else if (Ordenador === 2) {
        Ordenador = "Papel";
    }
    else {
        Ordenador = "Tijeras";
    }
}

document.write("<p>Ordenador:" + "" + Ordenador + "</p>");

//Funcion de comparaciones

var comparacion = function (eleccion1, eleccion2) {
    if (eleccion1 === eleccion2) {
        return "Empate!";
    }
    if (eleccion1 === "Piedra") {
        if (eleccion2 === "Tijeras") {
            return "Ganaste!"
        } else {
            return "Perdiste!"
        }
    }
    if (eleccion1 === "Piedra") {
        if (eleccion2 === "Papel") {
            return "Perdiste!"
        } else {
            return "Ganaste!"
        }
    }
    if (eleccion1 === "Tijeras") {
        if (eleccion2 === "Piedras") {
            return "Perdiste!"
        } else {
            return "Ganaste!"
        }
    }
    if (eleccion1 === "Tijeras") {
        if (eleccion2 === "Papel") {
            return "Ganaste!"
        } else {
            return "Perdiste!"
        }
    }    
    if (eleccion1 === "Papel") {
        if (eleccion2 === "Piedra") {
            return "Ganaste!"
        } else {
            return "Perdiste!"
        }
    }  
    if (eleccion1 === "Papel") {
        if (eleccion2 === "Tijeras") {
            return "Perdiste!"
        } else {
            return "Ganaste!"
        }
    } 
};    

if (eleccion1 === "Piedra") {
   if (eleccion2 === "Tijeras") {
       return "Ganaste!"
   } else {
       return "Perdiste!"
   }
}
if (eleccion1 === "Piedra") {
   if (eleccion2 === "Papel") {
       return "Perdiste!"
   } else {
       return "Ganaste!"
   }
}

I hope you do realize that this won’t work. If your first if statements has a success it will never reach the other second if statement… Your original comparacion code was correct. Your error was in the Ordenador function with the else having a condition.

As to your prompt. I have a hard time believing it will have a success when you enter ‘papel’ opposed to ‘Papel’. This must result in nothing. Since your comparicion function has no way to deal with this string.

I made some adjustments, build in a fail safe and error. Keep in mind, it now no longer uses html. All is done in the console. I kept to your code as much as possible but I believe a lot can be written shorter or more convenient.

let eleccionJugador;

while (eleccionJugador !== "papel" && eleccionJugador !== "tijeras" && eleccionJugador !== "piedra") {
    if (eleccionJugador) {
      console.log('Entrada incorrecta');
      console.log('------------------');
    }
    eleccionJugador = prompt("¿Papel, piedra o tijeras?");
    eleccionJugador = eleccionJugador.toLowerCase();
}

console.log("Tu eliges: " + eleccionJugador)

var ordenadorMates = function getRandomInt(minimo, maximo) {
  return Math.floor(Math.random() * (maximo - minimo )) + minimo;
}

var ordenador = ordenadorMates(1, 4); {
  if (ordenador === 1) {
    ordenador = "piedra";
  } else if (ordenador === 2) {
    ordenador = "papel";
  } else {
    ordenador = "tijeras";
  }
  console.log("ordenador: " + ordenador);
}

var comparacion = function(eleccion1, eleccion2) {
  if (eleccion1 === eleccion2) {
    return "Empate!";
  }
  if (eleccion1 === "piedra") {
    if (eleccion2 === "tijeras") {
      return "Ganaste!";
    } else {
      return "Perdiste!";
    }
  }
  if (eleccion1 === "tijeras") {
    if (eleccion2 === "papel") {
      return "Ganaste!";
    } else {
      return "Perdiste!"
    }
  }
  if (eleccion1 === "papel") {
    if (eleccion2 === "piedra") {
      return "Ganaste!"; 
    } else {
      return "Perdiste!";
    }
  }
};

//Usar funcion Comparacion y guardar resultado.
var resultados = comparacion(eleccionJugador, ordenador);
console.log(resultados);

Thanks for all, i’m learning from my mistakes. But i don’t really understand so well the next code and the concept safe and error:

let eleccionJugador;

while (eleccionJugador !== "papel" && eleccionJugador !== "tijeras" && eleccionJugador !== "piedra") {
    if (eleccionJugador) {
      console.log('Entrada incorrecta');
      console.log('------------------');
    }

You declare a let variable, then with while you are saying like “while the option the player choose, if is not false execute the {}”? I didn’t understand to well this lines.

Ok, let me attempt to explain it to you.

When you are asked by the prompt to enter anything, the game will not work if you type 'bananas'. You need to build in some kind of failsafe . Therefore I implemented a while loop to keep on asking for user input (using the prompt) as long as the user has not provided a correct choice (papel, tijeras or piedra).

while (eleccionJugador !== "papel" && eleccionJugador !== "tijeras" && eleccionJugador !== "piedra") {
   // do
   eleccionJugador = prompt("¿Papel, piedra o tijeras?");
}  

But from a usability point of view, it is nice to tell the user he is doing something wrong. This is called user feedback.

So I implemented an error message to notify the user he has provided a wrong input and prevented the game from running. I do this right before asking the user again for input.

I could put simply put this message in the loop. But this would mean the moment you start the script it would immediately give the error message. Therefore I used the if statement.

if (eleccionJugador) {
      console.log('Entrada incorrecta');
      console.log('------------------');
    }

The if statement inside the while loop checks if there is a value for eleccionJugador. If not it skips the error message and presents the prompt. In case you then provide a wrong answer, the next time the loop runs, eleccionJugador has a value and you do see the errormessage.

Do you see it?

Happy coding!