Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User input in Node.js

Tags:

I am writing a program which will create an array of numbers, and double the content of each array, and storing the result as key/value pair. Earlier, I had hardcoded the array, so everything was fine.

Now, I have changed the logic a bit, I want to take the input from users and then, store the value in an array.

My problem is that I am not able to figure out, how to do this using node.js. I have installed the prompt module using npm install prompt, and also, have gone through the documentation, but nothing is working.

I know that I am making a small mistake here.

Here's my code:

//Javascript program to read the content of array of numbers
//Double each element
//Storing the value in an object as key/value pair.

//var Num=[2,10,30,50,100]; //Array initialization

var Num = new Array();
var i;
var obj = {}; //Object initialization

function my_arr(N) { return N;} //Reads the contents of array


function doubling(N_doubled) //Doubles the content of array
{
   doubled_number = my_arr(N_doubled);   
   return doubled_number * 2;
}   

//outside function call
var prompt = require('prompt');

prompt.start();

while(i!== "QUIT")
{
    i = require('prompt');
    Num.push(i);
}
console.log(Num);

for(var i=0; i< Num.length; i++)
 {
    var original_value = my_arr(Num[i]); //storing the original values of array
    var doubled_value = doubling(Num[i]); //storing the content multiplied by two
    obj[original_value] = doubled_value; //object mapping
}

console.log(obj); //printing the final result as key/value pair

Kindly help me in this, Thanks.

like image 663
vamosrafa Avatar asked Jul 24 '13 14:07

vamosrafa


People also ask

How do you ask for user input in NodeJS?

Using prompt-sync const prompt = require('prompt-sync')(); Notice the extra () after require() . The prompt-sync module is a function that creates prompting functions, so you need to call prompt-sync in order to get your actual prompting function.

Is there user input in JavaScript?

In JavaScript, we use the prompt() function to ask the user for input. As a parameter, we input the text we want to display to the user. Once the user presses “ok,” the input value is returned. We typically store user input in a variable so that we can use the information in our program.

What is createInterface in NodeJS?

createInterface( process. stdin, process. stdout); The method createInterface() takes two parameters – the input stream and output stream – to create a readline interface.

What is readline () in JavaScript?

Readline Module in Node.js allows the reading of input stream line by line. This module wraps up the process standard output and process standard input objects. Readline module makes it easier for input and reading the output given by the user.


1 Answers

For those that do not want to import yet another module you can use the standard nodejs process.

function prompt(question, callback) {
    var stdin = process.stdin,
        stdout = process.stdout;

    stdin.resume();
    stdout.write(question);

    stdin.once('data', function (data) {
        callback(data.toString().trim());
    });
}

Use case

prompt('Whats your name?', function (input) {
    console.log(input);
    process.exit();
});
like image 92
Rick Avatar answered Sep 23 '22 19:09

Rick