Home javascript node js readline how to read all lines

node js readline how to read all lines

Author

Date

Category

There is such a task

go input

string 1
Line 2.
Row 3.
... etc.

This is not one line. So how to read all the lines?

An example of I / O for JS:

const readline = require ('readline');
Const RL = readline.createInterface (process.stdin, process.stdout);
RL.ON ('Line', (Line) = & gt; {
  // Entered line in the LINE variable, here you can write a solution
  Console.log (String (RESULT));
  RL.Close ();
  Return;
}) ON ('Close', () = & gt; process.exit (0));

Next, I need to handle the strings for example

line.split ("\ n");

But I only get the first string if you remove the rl.close () then the first line falls into the function. I can’t understand how to write to a variable or array all lines.
Thank you.


Answer 1

may be the reason in rl.close ();

So works as it should

const readline = require ('readline');
Const RL = readline.createInterface ({
 Input: Process.stdin,
 Output: Process.stdout.
});
RL.ON ('Line', line = & gt; {
  Console.log (Line);
}) ON ('Close', () = & gt; {
   Console.log ('exit');
   Process.exit (0);
});

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions