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

93
Views
Cómo salir de process.stdin.on y luego volver a abrir process.stdin.on

Tengo problemas con un ejercicio. El cliente ingresa un cierto número entero x, de acuerdo con el número entero x, ingresará x otros valores y, separados por espacios. La salida será la suma de cada y en sí misma, separada por espacios.

Ejemplo:

 Input: 4 | Input: 5 21 35 55 66 | 333 53 99 12 55 Output:3 8 10 12 | Output: 9 8 18 3 10

y así...

Hasta ahora todo bien, mi problema es que no entiendo process.stdin.on('data', function() ... )

No encontré muchos videos o explicaciones sobre esto.

Por lo que entendí de 2 videos cortos de explicaciones, hice esto:

 let number = null; let numOrder = []; function clientsnum() { process.stdout.write("Enter number: "); process.stdin.on("data", function (data) { number = parseInt(data.toString().trim()); process.stdin.pause(); }); process.stdin.on("pause", function () { console.log("Paused"); process.stdout.write(`${numOrder} ${number}` + "\n"); //for debug order(); }); } function order() { console.log("got here"); //for debug process.stdout.write("numberOrder: "); process.stdin.resume(); process.stdin.on("data", function (data) { if (numOrder.length < number) { numOrder.push(parseInt(data.toString().trim())); } else { process.exit(); } }); process.on('exit', function() { console.log('Over') }) } clientsnum();

A veces, puedo ver que se duplica en la matriz. Además, cambia la declaración anterior de "número".

El ejercicio nos muestra una plantilla donde vi en otras páginas, por código puedo entender pero por práctica no funciona cuando lo pruebo "process.stdin.on('end', ...)".

Si alguien quiere probar que lo explique también. Agradecería.

Aquí está la plantilla:

 function main(input) { process.stdout.write("hello " + input) //write output } process.stdin.resume() process.stdin.setEncoding("utf-8") var stdin_input = "" process.stdin.on("data", function(input) { stdin_input += input //read input from stdin }) process.stdin.on("end", function (){ main(stdin_input) })
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

En mi segundo día, después de una buena ducha pensando. Tengo una solución.

 let numOfClient = null; let orderID = []; let answers = []; let questions = ["Enter number of clients: ", "Order ID: "]; function question() { process.stdout.write(questions[0]); process.stdin.on("data", function (data) { answers.push(parseInt(data.toString().trim())); numOfClient = answers[0]; if (answers.length < numOfClient + 1) { process.stdout.write(questions[1]); // Here the question is not answered yet if (answers.length !== 1) { // So I need to wait for the next interation/loop orderID.push(answers[answers.length - 1]); } } else { orderID.push(answers[answers.length - 1]); // To get the last loop answer process.exit(); } }); process.on("exit", function () { // for debug console.log( `Done, the answers [${answers}] , the numberOfClient ${numOfClient}, the orderID's [${orderID}] ` ); }); } question();
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!