This is some homework I have. I want it to read user input and then create two objects, giving them values based on the user input. Here is my code, beginning at the top of the JS file:
console.log("Type 'help' for commands");
console.log("Square numbers:");
console.log("Enter player 1 name");
var player1 = new Player(readLine());
var player2 = new Player(readLine());
console.log("logging name after making players" + player1.name);
function readLine() {
var inputText;
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("Enter player name? ", function(answer) {
inputText = answer;
console.log("Player name:", answer);
rl.pause();
});
return inputText;
}
Here is the output I get: http://oi60.tinypic.com/9tn7nn.jpg
I'm used to C# and Java and in those languages, readline() would wait for the input and then create the object with whatever I inputted, before moving onto the next line. Not in JS though! It just carries on, calling readline() twice before I've typed anything in (therefore printing "prompt>" twice) and then logging player 1's name (before typing it in, hence undefined). Then it waits for me to type something in and once I type something, it sets both players to the one name and then ends the program.
Is it possible to do what I am trying to get it to do? Is there another way I can get input to work without it skipping past it and running the rest of the code?
Thanks in advance!
readlineor do you have to write it yourself for the assignment?syshas been deprecated for a very long time, please useutil!