Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

192
Views
¿Cómo obtengo el retorno de un resultado de una victoria o una pérdida en javascript

¿Cómo actualizaría la puntuación del juego después de cada resultado? Esto es algo en lo que he estado trabajando en la consola como parte de un proyecto escolar y pasé demasiado tiempo tratando de atar esto. Logré que los resultados se muestren en la consola como quién es el ganador según el cambio y los casos, pero ¿cómo tomo ese resultado extraído del texto de cadena y hago algo que actualizará la matriz del juego? Lo siento si no estoy haciendo la pregunta correcta

 const game = { win: 0, loss: 0, }; const choices = ["rock", "paper", "scissors"]; var random1 = Math.round(Math.random() * 2); var random2 = Math.round(Math.random() * 2); var bot1 = choices[random1]; var bot2 = choices[random2]; console.log("Player 1:" + bot1); console.log("Player 2:" + bot2); var result = ""; function returnResult() { switch (bot1) { case "paper": switch (bot2) { case "rock": console.log("Player 1 Wins!"); result = win++; break; case "scissors": console.log("Player 2 Wins!"); result = loss++; break; case "paper": console.log("Draw!"); break; } } switch (bot1) { case "rock": switch (bot2) { case "scissors": console.log("Player 1 Wins!"); result = win++; break; case "paper": console.log("Player 2 Wins!"); result = loss++; break; case "rock": console.log("Draw!"); break; } } switch (bot1) { case "scissors": switch (bot2) { case "paper": console.log("Player 1 Wins!"); result = win++; break; case "rock": console.log("Player 2 Wins!"); result = loss++; break; case "scissors": console.log("Draw!"); break; } } return result; } console.log(results);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

No estoy seguro de cómo pretende jugar el juego, pero aquí hay una sugerencia sobre cómo llevar la puntuación en su objeto de game . Presiona el botón Reproducir para comenzar el juego y mantener la puntuación en game :

 const game = { win: 0, loss: 0, }; const choices = ["rock", "paper", "scissors"]; function returnResult() { console.clear() // This is just to prevent console crowding for this example // Move your random number generators inside the function // so you get a new random number each time game is played. const random1 = Math.round(Math.random() * 2); const random2 = Math.round(Math.random() * 2); const bot1 = choices[random1]; const bot2 = choices[random2]; console.log("Player 1:" + bot1); console.log("Player 2:" + bot2); // var result = ""; You don't this if you are keeping score in `game` object // No need to call switch on bot1 three times: switch (bot1) { case "paper": switch (bot2) { case "rock": console.log("Player 1 Wins!"); game.win++; // Update game win break; case "scissors": console.log("Player 2 Wins!"); game.loss++; // Update game loss break; case "paper": console.log("Draw!"); break; } break; case "rock": switch (bot2) { case "scissors": console.log("Player 1 Wins!"); game.win++; // Update game win break; case "paper": console.log("Player 2 Wins!"); game.loss++; // Update game loss break; case "rock": console.log("Draw!"); break; } break; case "scissors": switch (bot2) { case "paper": console.log("Player 1 Wins!"); game.win++; // Update game win break; case "rock": console.log("Player 2 Wins!"); game.loss++; // Update game loss break; case "scissors": console.log("Draw!"); break; } break; } // return result; No need to return result. Update game object every play instead. console.log(game) return game // Return game object if needed }
 <button onclick="returnResult()">Play</button>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!