'How to exit process.stdin.on and then reopen process.stdin.on

I have struggling with an exercise. The client input a certain integer x, according with the integer x they will input x other y values, space-separated. The output will be the addition of each y itself, space-separated.

Example:

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

and so on...

So far so good, my problem is that I don't understand process.stdin.on('data', function() ... )

I didn't find many videos or explanations about this.

From what I understood from 2 short videos of explanations, I did this:

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();

Sometimes, I can see it doubles on the array. Also, it changes the previous statement of "number".

The exercise show us a template where I saw in other pages, by code I can understand but by practice it doesn't work when I try it "process.stdin.on('end', ...)".

If someone wants to try explain it too. I would appreciate.

Here is the template:

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)
})


Solution 1:[1]

On my second day, after a good shower thinking. I got one solution.

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();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 IndexError