Estoy haciendo este juego de piedra, papel y tijera, sin embargo, mi última función no funciona correctamente, no ejecutará ese bloque de código, y lo que está mal con las variables en la última función, no se pueden llamar, Intenté cambiar los parámetros, también depuré el código y, en la medida en que noté que solo la última función no funciona correctamente.
const getUserChoice = userInput => { userInput = userInput.toLowerCase(); if (userInput == "rock" || userInput == "paper" || userInput == "scissors") { return userInput; } else { console.log('Enter a valid option') }; }; function getComputerChoice(any) { return Math.floor(Math.random() * any) } switch (getComputerChoice(3)) //userInput { case 0: console.log('rock'); break; case 1: console.log('paper'); break; case 2: console.log('scissors'); break; default: console.log('Select a valid option') } function determineWinner(getUserChoice, getComputerChoice) { if (getUserChoice === getComputerChoice) { return 'The game was a tie' } if (getUserChoice === 'rock') { if (getComputerChoice === 'paper') { return 'The computer won!' } else { return 'You won' } } if (getUserChoice === 'scissors') { if (getComputerChoice === 'rock') { return 'Computer won!' } else { return 'You won!' } } } const playGam = () => { const userChoice = getUserChoice('scissors'); const computerChoice = getComputerChoice(); console.log('You threw: ' + userChoice); console.log('The computer threw: ' + computerChoice); console.log(determineWinner(userChoice, computerChoice)); } playGam();Tal como se publicó, getComputerChoice simplemente hace cálculos aleatorios y los multiplica por un parámetro (que a veces falta). Considere cambiar para devolver una elección aleatoria del conjunto de ['rock', 'paper', 'scissors'] ...
function getComputerChoice() { const choices = ['rock', 'paper', 'scissors']; return choices[Math.floor(Math.random() * choices.length)] } const getUserChoice = userInput => { userInput = userInput.toLowerCase(); if (userInput == "rock" || userInput == "paper" || userInput == "scissors") { return userInput; } else { console.log('Enter a valid option') }; }; function getComputerChoice() { const choices = ['rock', 'paper', 'scissors']; return choices[Math.floor(Math.random() * choices.length)] } switch (getComputerChoice(3)) //userInput { case 0: console.log('rock'); break; case 1: console.log('paper'); break; case 2: console.log('scissors'); break; default: console.log('Select a valid option') } function determineWinner(getUserChoice, getComputerChoice) { if (getUserChoice === getComputerChoice) { return 'The game was a tie' } if (getUserChoice === 'rock') { if (getComputerChoice === 'paper') { return 'The computer won!' } else { return 'You won' } } if (getUserChoice === 'scissors') { if (getComputerChoice === 'rock') { return 'Computer won!' } else { return 'You won!' } } } const playGam = () => { const userChoice = getUserChoice('scissors'); const computerChoice = getComputerChoice(); console.log('You threw: ' + userChoice); console.log('The computer threw: ' + computerChoice); console.log(determineWinner(userChoice, computerChoice)); } playGam();También podríamos mejorar el código de otras maneras...
// make this global const choices = ['rock', 'paper', 'scissors']; const beats = { rock: 'scissors', scissors: 'paper', paper: 'rock' }; // general purpose fn to validate const validateChoice = choice => { return choices.includes(choice.toLowerCase()); }; // a tightly specified thing to determine winner const determineWinner(userChoice, computerChoice) { if (computerChoice === userChoice) return 'tie'; return beats[userChoice] === computerChoice ? 'user win' : 'computer win'; }No está configurando un valor variable con su declaración de cambio. Debe devolver la elección de la computadora, así que puse su declaración de cambio dentro de la función getComputerChoice y luego devuelve la salida y puede usarla en su función playGam . También actualicé eso para mostrar el ganador en cada resultado.
const getUserChoice = userInput => { userInput = userInput.toLowerCase(); if (userInput == "rock" || userInput == "paper" || userInput == "scissors") { return userInput; } else { console.log('Enter a valid option') }; }; function getComputerChoice(any) { const randomNo = Math.floor(Math.random() * any) switch (randomNo) //userInput { case 0: return 'scissors'; break; case 1: return 'paper'; break; case 2: return 'rock'; break; default: return 'Select a valid option'; } } function determineWinner(getUserChoice, getComputerChoice) { if (getUserChoice === getComputerChoice) { return 'The game was a tie' } switch (getUserChoice) { case 'paper': return getComputerChoice === 'scissors' ? 'The computer won!' : 'You won'; case 'rock' : return getComputerChoice === 'scissors' ? 'The computer won!' : 'You won'; case 'scissors' : return getComputerChoice === 'paper' ? 'You won' : 'The computer won!'; default: return 'Something went wrong' } } const playGam = () => { const userChoice = getUserChoice('paper'); const computerChoice = getComputerChoice(3); console.log('You threw: ' + userChoice); console.log('The computer threw: ' + computerChoice); console.log(determineWinner(userChoice, computerChoice)); } playGam();