How to use “readline()” function in JavaScript?

How to use “readline()” function in JavaScript?
0

#1
var names = ["David", "Cynthia", "Raymond", "Clayton", "Jennifer"]; putstr("Enter a name to search for: ");
var name = readline();
var position = names.indexOf(name);
if (position >= 0) {
print("Found " + name + " at position " + position);
} else {
       print(name + " not found in array.");
    }


#2

If you are talking about Node’s readline method, then read this.


#3

No, I run into Data Structures and Algorithm with JS books. It was not giving any information about Node’s. I just need help the second line in my sharing code. Thanks


#4

Is this a front-end or back-end project?

If front-end, what are you trying to do with readline in your code?


#5

only the second line. What is the functionality readline in the second line?

var names = ["David", "Cynthia", "Raymond", "Clayton", "Jennifer"]; putstr("Enter a name to search for: ");
var name = readline();
var position = names.indexOf(name);
if (position >= 0) {
print("Found " + name + " at position " + position);
} else {
       print(name + " not found in array.");
    }


#6

Hi @snntaylan,
This looks like code for SpiderMonkey, the Command Line JavaScript runtime environment from Mozilla.
readline() is their method to read stdin and print() is the stdout method.
If you come from web, you could almost compare readline to prompt(), the script simply pauses and waits until something comes from user input.

When you run this script in your console, the Spidermonkey runtime will start and a cursor will blink, waiting for your input (stdin). If you type or paste something and press enter (or get input from somewhere else) the script will continue with the line var position = names.indexOf(name);. Save the script to find_name.js.
If you have a Mac, SpiderMonkey comes preinstalled. Just open Terminal and write:

$ js find_name.js
Enter a name to search for: 

And when you type a name and press return:

Enter a name to search for: Johan
Johan not found in array. 
$