Hola comunidad de StackOverflow,
He estado estudiando JavaScript durante aproximadamente 3 días más o menos y he creado un "juego" de piedra, papel o tijera que se ejecuta pero solo se agrega al código con la decisión del jugador. En su lugar, quería arreglar esto para que apareciera una alerta emergente que preguntara "Elige piedra, papel o tijera ahora" y que el juego usara esa entrada como el factor decisivo para decidir quién ganará. Ejecuté el código que proporcioné y terminé con este error:
a internal/modules/cjs/loader.js:638 throw err; ^ Error: Cannot find module 'readline-sync' at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15) at Function.Module._load (internal/modules/cjs/loader.js:562:25) at Module.require (internal/modules/cjs/loader.js:692:17) at require (internal/modules/cjs/helpers.js:25:18) at Object.<anonymous> (/tmp/zUokdwLcHz.js:1:20) at Module._compile (internal/modules/cjs/loader.js:778:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10) at Module.load (internal/modules/cjs/loader.js:653:32) at tryModuleLoad (internal/modules/cjs/loader.js:593:12) at Function.Module._load (internal/modules/cjs/loader.js:585:3)Traté de revisar mi ortografía y ()/;/, para asegurarme de que todos son correctos y siento que lo son. Estoy seguro de que esta es una solución fácil, así que gracias de antemano si me puede guiar de la manera correcta.
Aquí está el código:
const userInput = prompt("What will you shoot") userInput = userInput.toLowerCase(); if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') { return userInput; } else { console.log("Error!"); } const ComputerChoice = () => { const randomNumber = Math.floor(Math.random() * 3) if (randomNumber === 0) { return "rock"; } else if (randomNumber === 1) { return "paper"; } else { return "scissors"; } } const determineWinner = (userChoice, computerChoice) => { if (userChoice === computerChoice) { return "Tie, please try again"; } else if (userChoice === "paper" && computerChoice === "rock") { return "Player wins"; } else if (userChoice === "scissors" && computerChoice === "Paper") { return "Player wins"; } else if (userChoice === "rock" && computerChoice === "scissors") { return "Player wins"; } else if (userChoice === "bomb") { return "Player wins" } else { return "Computer Wins" } } const playGame = () => { const userChoice = getUserChoice('') const computerChoice = ComputerChoice() console.log("you threw a " + userChoice); console.log("The computer threw a " + computerChoice); console.log(determineWinner(userChoice, computerChoice)); } playGame();comente su primera declaración de devolución: es ilegal y cambie getUserInput () en la parte inferior a solo entrada de usuario
var userInput = prompt("What will you shoot") userInput = userInput.toLowerCase(); if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') { //return userInput; } else { console.log("Error!"); } const ComputerChoice = () => { const randomNumber = Math.floor(Math.random() * 3) if (randomNumber === 0) { return "rock"; } else if (randomNumber === 1) { return "paper"; } else { return "scissors"; } } const determineWinner = (userChoice, computerChoice) => { if (userChoice === computerChoice) { return "Tie, please try again"; } else if (userChoice === "paper" && computerChoice === "rock") { return "Player wins"; } else if (userChoice === "scissors" && computerChoice === "Paper") { return "Player wins"; } else if (userChoice === "rock" && computerChoice === "scissors") { return "Player wins"; } else if (userChoice === "bomb") { return "Player wins" } else { return "Computer Wins" } } const playGame = () => { const userChoice = userInput//getUserChoice('') const computerChoice = ComputerChoice() console.log("you threw a " + userChoice); console.log("The computer threw a " + computerChoice); console.log(determineWinner(userChoice, computerChoice)); } playGame();