Is there a way to read standard input with JavaScript?

后端 未结 5 612
名媛妹妹
名媛妹妹 2020-12-08 20:09

I saw this for lots of other languages but not JavaScript.

I\'m trying to do problems like: this (codechef.com) and of course the programs need to be able to read

5条回答
  •  星月不相逢
    2020-12-08 20:39

    If you use node to act as an interpreter in the terminal, you can use this:

    ---- name.js ----
    var readline = require('readline');
    
    var rl = readline.createInterface({
      input: process.stdin,
      output: process.stdout
    
    });
    
    rl.question(">>What's your name?  ", function(answer) {
       console.log("Hello " + answer);
       rl.close();
    });
    
    ----- terminal ----
    node name.js
    

提交回复
热议问题