3

I want to ask the user through the console to enter a number. I then want to store it into a variable to have it used for a function etc. Here is an example of what I have done. When i do this it says ReferenceError: readline is not defined

console.log("Enter your guess and press <Enter>: /t" );
var userNumber = readline();

I am just trying to create an interactive js console app for now.

Edit: Here is my source code http://repl.it/NeI/4

Beast_Code
  • 3,097
  • 12
  • 43
  • 54

3 Answers3

12

Try doing this:

var guess = window.prompt("Enter your guess", "Number from 1 to 9");
console.log(guess);
Alex Shilman
  • 1,547
  • 1
  • 11
  • 15
3

The console does not do what you are hoping it will in this situation.

Here is the API documentation from Mozilla, and here are the Chrome docs.

The console intent is to display information from your application for debugging purposes, with some minor helper functions.

Kelly J Andrews
  • 5,083
  • 1
  • 19
  • 32
0

Developer tools or Firebug console can't receive user data this way, there's no read or readline methods. You have to use a window method called "prompt" .

var data = prompt("Type something to me baby!");
console.log(data);

Here's another question addressing this same doubt.

Community
  • 1
  • 1
Alcides Queiroz
  • 9,456
  • 3
  • 28
  • 43