2

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 Shell according to MDN. But the function is not defined when I run the code in my browser.

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.

7
  • 3
    From where are you expecting to read? You do not have a file. Browser based JS does not have stdin - or are you talking node.js? Commented Mar 10, 2017 at 15:27
  • You can't read from stdin in javascript Commented Mar 10, 2017 at 15:28
  • 1
    It's for the js shell command line program which has nothing to do with js within a web browser. Where would it even read from? Commented Mar 10, 2017 at 15:28
  • 2
    The function, and the mdn page you link to, are about writing programs for the JavaScript shell, jsc. You can't run it in a browser. Commented Mar 10, 2017 at 15:29
  • 1
    @mplungjan its jsc. Predates (and largely superceded by) node. Commented Mar 10, 2017 at 15:31

1 Answer 1

1

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

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.