如何在 JavaScript 中使用“readline()”函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42721943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:23:39  来源:igfitidea点击:

How to use "readline()" function in JavaScript?

javascript

提问by lbs0912

I was learning 去哪儿网-2017笔试题and programing. In my program, I want to use readline()function to read a single line from input from stdin. I know that readline()function belongs to JavaScript Shellaccording to MDN. But the function is not defined when I run the code in my browser.

我在学习去哪儿网-2017笔试题和编程。在我的程序中,我想使用readline()函数从stdin. 根据MDN,我知道该 readline()函数属于JavaScript Shell。但是当我在浏览器中运行代码时没有定义该函数。

var line;
while (line = read_line()) {
  while (line.indexOf(" ") != -1) {
    line = line.replace(" ", "");
  }
  if (line.length <= 6) {
    print(line);
  } else if (line.length > 6 && line.length <= 14) {
    var line1 = line.substring(0, 6);
    var line2 = line.substring(6);
    print(line1 + " " + line2);
  } else if (line.length > 14 && line.length <= 18) {
    var line1 = line.substring(0, 6);
    var line2 = line.substring(6, 14);
    var line3 = line.substring(14);
    print(line1 + " " + line2 + " " + line3);
  }
}

If I want to run the code in my browser. What should I do?

如果我想在浏览器中运行代码。我该怎么办?

Thank you very much.

非常感谢。

回答by Ezequiel De Simone

As you mentioned above and the documentation says Introduction to the JavaScript shell

正如你上面提到的,文档说JavaScript shell 简介

it is a command line options and you have to run that over the command shell. Also, you can check SpiderMonkeyto have a better understanding.

它是一个命令行选项,您必须在命令外壳上运行它。此外,您可以查看SpiderMonkey以更好地了解。