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

202
Views
¿Cómo tomar la entrada del usuario en el nodo js?

Estoy tratando de escribir la lógica del juego de tic-tac-toe, por lo que necesito tomar las entradas del usuario alternativamente del usuario 'X y el usuario 'O. El primer jugador es elegido por una función aleatoria.

 const prompt = require('prompt-sync')({ sigint: true }); const ticTacToe = { board: new Array(9).fill(null), person: null, player: function () { let number = Math.floor(Math.random() * 2); if (number === 0) { return this.person = 'X'; } else { return this.person = 'O'; } }, start: function () { let firstPlayer = this.person; let count = 0; while (count <= 9) { let input; if (count === 0) { input = prompt(firstPlayer + ": "); count = count + 1; } else { let nextPerson = this.nextPlayer(); input = prompt(nextPerson + ": "); count = count + 1; } } }, nextPlayer: function () { let next; if (this.person === 'X') { return next = 'O'; } else { return next = 'X'; } } }

El aviso no cambia y no termina cuando el conteo llega a 9. La salida se parece a:

 X: 7 X: 7 5 X: 7 5 0 X: 7 5 0
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

La siguiente solución funcionó para mí:

 node --version v10.19.0
 const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); function readLineAsync(message) { return new Promise((resolve, reject) => { rl.question(message, (answer) => { resolve(answer); }); }); } // Leverages Node.js' awesome async/await functionality async function demoSynchronousPrompt() { var promptInput = await readLineAsync("Give me some input >"); console.log("Won't be executed until promptInput is received", promptInput); rl.close(); }
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!