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;
}







0















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.










share|improve this question




















  • 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


















0















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.










share|improve this question




















  • 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














0












0








0


1






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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 its jsc. Predates (and largely superceded by) node.

    – Jared Smith
    Mar 10 '17 at 15:31














  • 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








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












1 Answer
1






active

oldest

votes


















0














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.






share|improve this answer
























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    0














    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.






    share|improve this answer




























      0














      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.






      share|improve this answer


























        0












        0








        0







        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jun 19 '17 at 14:12









        3p3ch33p3ch3

        1418




        1418
































            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Mossoró

            Error while reading .h5 file using the rhdf5 package in R

            Pushsharp Apns notification error: 'InvalidToken'