How to resolve the algorithm Input loop step by step in the JavaScript programming language
How to resolve the algorithm Input loop step by step in the JavaScript programming language
Table of Contents
Problem Statement
Read from a text stream either word-by-word or line-by-line until the stream runs out of data. The stream will have an unknown amount of data on it.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Input loop step by step in the JavaScript programming language
This JavaScript code reads lines of text from the standard input stream and prints them to the standard output stream, prefixing each line with a line number.
The code uses the WScript
object, which is available in Windows Script Host (WSH), to access the standard input and output streams.
The WScript.StdIn
property returns a TextStream
object that represents the standard input stream. The TextStream
object has a ReadLine()
method that reads a single line of text from the stream.
The WScript.echo()
method prints a string to the standard output stream.
The code uses a while
loop to read lines of text from the standard input stream until the end of the stream is reached. The AtEndOfStream
property of the TextStream
object returns true
if the end of the stream has been reached.
For each line of text that is read, the code increments the line number and prints the line number and the line of text to the standard output stream.
Source code in the javascript programming language
var text_stream = WScript.StdIn;
var i = 0;
while ( ! text_stream.AtEndOfStream ) {
var line = text_stream.ReadLine();
// do something with line
WScript.echo(++i + ": " + line);
}
You may also check:How to resolve the algorithm XML/XPath step by step in the Lua programming language
You may also check:How to resolve the algorithm Loops/Infinite step by step in the C programming language
You may also check:How to resolve the algorithm Largest int from concatenated ints step by step in the Perl programming language
You may also check:How to resolve the algorithm Balanced brackets step by step in the Euphoria programming language
You may also check:How to resolve the algorithm Rosetta Code/Rank languages by popularity step by step in the Phix programming language