Last active
December 26, 2022 00:13
-
-
Save ntourne/a5a4a9c6f1e094a49d36fbcacbd82a58 to your computer and use it in GitHub Desktop.
rock-paper-scissors-game.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// It asks a user to pick an option and returns the correct value | |
// ("Piedra", "Papel" or "Tijera") or an "Error" | |
function getOptionPicked(playerName) { | |
const option = prompt( | |
`Seleccione opción para el ${playerName}:\n1: Piedra\n2: Papel\n3: Tijera` | |
); | |
let pickedOption = null; | |
switch (option) { | |
case "1": | |
pickedOption = "Piedra"; | |
break; | |
case "2": | |
pickedOption = "Papel"; | |
break; | |
case "3": | |
pickedOption = "Tijera"; | |
break; | |
default: | |
pickedOption = "ERROR"; | |
} | |
return pickedOption; | |
} | |
// It receives the values picked by the players and returns the result | |
function getResult(optionPlayer1, optionPlayer2) { | |
let result = ""; | |
if (optionPlayer1 === "ERROR" || optionPlayer2 === "ERROR") { | |
result = | |
"No se puede calcular el resultado porque ingresaron alguna opción incorrecta"; | |
} else if (optionPlayer1 === optionPlayer2) { | |
result = "Empataron"; | |
} else if ( | |
(optionPlayer1 === "Piedra" && optionPlayer2 === "Tijera") || | |
(optionPlayer1 === "Papel" && optionPlayer2 === "Piedra") || | |
(optionPlayer1 === "Tijera" && optionPlayer2 === "Papel") | |
) { | |
result = `Ganó el Jugador 1: ${optionPlayer1} le gana a ${optionPlayer2}`; | |
} else { | |
result = `Ganó el Jugador 2: ${optionPlayer2} le gana a ${optionPlayer1}`; | |
} | |
return result; | |
} | |
// It manages all the steps in the game | |
function main() { | |
let isGameAvailable = true; | |
do { | |
const optionPlayer1 = getOptionPicked("Jugador 1"); | |
const optionPlayer2 = getOptionPicked("Jugador 2"); | |
const result = getResult(optionPlayer1, optionPlayer2); | |
alert(result); | |
// It asks if the users wants to play again | |
isGameAvailable = confirm("¿Querés volver a jugar?"); | |
} while (isGameAvailable); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment