How to use “readline()” function in JavaScript?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
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.
javascript
|
show 2 more comments
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.
javascript
2
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?
– mplungjan
Mar 10 '17 at 15:27
You can't read from stdin in javascript
– Dave Clough
Mar 10 '17 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?
– Alex K.
Mar 10 '17 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.
– Jared Smith
Mar 10 '17 at 15:29
1
@mplungjan itsjsc. Predates (and largely superceded by) node.
– Jared Smith
Mar 10 '17 at 15:31
|
show 2 more comments
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.
javascript
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.
javascript
javascript
edited Jan 13 '18 at 18:21
antzshrek
3,00031735
3,00031735
asked Mar 10 '17 at 15:25
lbs0912lbs0912
961111
961111
2
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?
– mplungjan
Mar 10 '17 at 15:27
You can't read from stdin in javascript
– Dave Clough
Mar 10 '17 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?
– Alex K.
Mar 10 '17 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.
– Jared Smith
Mar 10 '17 at 15:29
1
@mplungjan itsjsc. Predates (and largely superceded by) node.
– Jared Smith
Mar 10 '17 at 15:31
|
show 2 more comments
2
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?
– mplungjan
Mar 10 '17 at 15:27
You can't read from stdin in javascript
– Dave Clough
Mar 10 '17 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?
– Alex K.
Mar 10 '17 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.
– Jared Smith
Mar 10 '17 at 15:29
1
@mplungjan itsjsc. Predates (and largely superceded by) node.
– Jared Smith
Mar 10 '17 at 15:31
2
2
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?
– mplungjan
Mar 10 '17 at 15:27
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?
– mplungjan
Mar 10 '17 at 15:27
You can't read from stdin in javascript
– Dave Clough
Mar 10 '17 at 15:28
You can't read from stdin in javascript
– Dave Clough
Mar 10 '17 at 15:28
1
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?
– Alex K.
Mar 10 '17 at 15:28
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?
– Alex K.
Mar 10 '17 at 15:28
2
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.– Jared Smith
Mar 10 '17 at 15:29
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.– Jared Smith
Mar 10 '17 at 15:29
1
1
@mplungjan its
jsc. Predates (and largely superceded by) node.– Jared Smith
Mar 10 '17 at 15:31
@mplungjan its
jsc. Predates (and largely superceded by) node.– Jared Smith
Mar 10 '17 at 15:31
|
show 2 more comments
1 Answer
1
active
oldest
votes
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.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42721943%2fhow-to-use-readline-function-in-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
answered Jun 19 '17 at 14:12
3p3ch33p3ch3
1418
1418
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42721943%2fhow-to-use-readline-function-in-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
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?
– mplungjan
Mar 10 '17 at 15:27
You can't read from stdin in javascript
– Dave Clough
Mar 10 '17 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?
– Alex K.
Mar 10 '17 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.– Jared Smith
Mar 10 '17 at 15:29
1
@mplungjan its
jsc. Predates (and largely superceded by) node.– Jared Smith
Mar 10 '17 at 15:31