Hola a todos, esta es mi primera pregunta aquí, lo siento si no coincide con todas las reglas. Estoy tratando de hacer un juego de piedra, papel o tijera del Proyecto Odin en javascript, y tengo problemas con el resultado porque no muestra lo que debería. Si la computadora toca rock para e. y toco rock, debería obtener un empate, pero obtengo algo al azar todo el tiempo. El código actual es con aviso () para la entrada del usuario, pero traté de poner una cadena fija como "papel" y fue lo mismo, también pensé que era porque no puse toLowerCase() correctamente, así que lo eliminé por completo y es sigue igual, agradeceria su ayuda!
function computerPlay() { let rps = ["rock", "paper", "scissors"]; let random = rps[Math.floor(Math.random() * rps.length)]; return random; } console.log(computerPlay()); function playRound(playerSelection, computerSelection) { if (playerSelection === computerSelection) { return ("It's a draw!"); } else if ((playerSelection === "rock") && (computerSelection === "scissors")) { return ("You win! Rock beats scissors"); } else if (playerSelection === "rock" && computerSelection === "paper") { return ("You lose! Paper beats rock"); } else if (playerSelection === "paper" && computerSelection === "rock") { return ("You win! Paper beats rock"); } else if (playerSelection === "paper" && computerSelection === "scissors") { return ("You lose! Scissors beat paper"); } else if (playerSelection === "scissors" && computerSelection === "paper") { return ("You win! Scissors beat paper"); } else if (playerSelection === "scissors" && computerSelection === "rock") { return ("You lose!Rock beats scissors"); } } let computerSelection = computerPlay(); let playerSelection = prompt("Choose your weapon"); console.log(playRound(playerSelection, computerSelection));Primero console.log la función computerPlay() y eso devuelve un cierto valor. Después de eso, juegas la ronda y vuelves a llamar a la función computerPlay(), esta calcula la ronda con un valor diferente al que usaste primero en console.log.
Prueba este código a continuación
function computerPlay() { let rps = ["rock", "paper", "scissors"]; let random = rps[Math.floor(Math.random() * rps.length)]; return random; } function playRound(playerSelection, computerSelection) { if (playerSelection === computerSelection) { return ("It's a draw!"); } else if ((playerSelection === "rock") && (computerSelection === "scissors")) { return ("You win! Rock beats scissors"); } else if (playerSelection === "rock" && computerSelection === "paper") { return ("You lose! Paper beats rock"); } else if (playerSelection === "paper" && computerSelection === "rock") { return ("You win! Paper beats rock"); } else if (playerSelection === "paper" && computerSelection === "scissors") { return ("You lose! Scissors beat paper"); } else if (playerSelection === "scissors" && computerSelection === "paper") { return ("You win! Scissors beat paper"); } else if (playerSelection === "scissors" && computerSelection === "rock") { return ("You lose!Rock beats scissors"); } } let computerSelection = computerPlay(); let playerSelection = prompt("Choose your weapon"); console.log(computerSelection) console.log(playRound(playerSelection, computerSelection));Puede simplificar sus condiciones de esta manera
const choices = ["Rock", "Paper", "Scissors"] const computerPlay = () => choices[Math.floor(Math.random() * choices.length)] function playRound(playerSelection, computerSelection) { const difference = (choices.length + choices.indexOf(playerSelection) - choices.indexOf(computerSelection) )% choices.length switch(difference){ case 0: return "It's a draw!" case 2: return `You lose! ${computerSelection} beats ${playerSelection}` default: return `You win! ${playerSelection} beats ${computerSelection}` } } let computerSelection = computerPlay(); let playerSelection while(!choices.includes(playerSelection)){ const selected = prompt("Choose your weapon").trim().toLowerCase(); playerSelection = selected[0].toUpperCase()+selected.slice(1) } console.log(playRound(playerSelection, computerSelection));