9

I'm planning on using JavaScript to enter an informatics competition (BIO) tomorrow. However, I can't rely on the examiner having a browser with a decent JavaScript engine, so I was hoping to use Microsoft's JScript instead.

However, the documentation is, quite frankly, crap. Can someone post some example code that reads in a line of text, calls foo(string) on it, and echos the output to the command line?

Similarly, how do I actually run it? Will wscript.exe PATH_TO_JS_FILE do the trick?

Eric
  • 87,154
  • 48
  • 211
  • 332

2 Answers2

14

If you're using the command-line, I'd execute the script using CSCRIPT.EXE. ie: CSCRIPT.EXE myscript.js This is because WScript.Echo from WSCRIPT will create a dialog box and from CSCRIPT outputs a line to the console. Run this in a command window (CMD).

Reading a line from console into variable:

var x = WScript.StdIn.ReadLine();

Where StdIn is a TextStream object. There is also an StdOut which can be used in place of WScript.Echo()...

Writing the output of foo(x) to console: (must run under CSCRIPT)

WScript.Echo(foo(x));

You can use the WScript object to determine which engine you are running under, there's a question/answer for that (VBScript, but uses the same objects under JScript) here.

Community
  • 1
  • 1
aikeru
  • 3,593
  • 3
  • 30
  • 44
2

If you want to be sure your program only runs in the command line, you may use the WScript.StdIn and WScript.StdOut objects/properties:

var myString = WScript.StdIn.ReadLine();
WScript.StdOut.WriteLine(myString);

and run it with cscript.exe. But if you want it to be a GUI program, it is a bit more difficult considering that JScript doesn't have a native InputBox function like VBScript. However, as described here we may use Windows Script Host (WSH). Create a .wsf file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<job id="testJScriptInputBox">
    <script language="VBScript">
    <![CDATA[
        Function WSHInputBox(Message, Title, Value)
            WSHInputBox = InputBox(Message, Title, Value)
        End Function
    ]]>
    </script>

    <script language="JScript">
    <![CDATA[
        var vbOKOnly = 0;                // Constants for Popup
        var vbInformation = 64;

        var title = "InputBox function for JScript";
        var prompt = "Enter a string: ";

        var WshShell = WScript.CreateObject("WScript.Shell");

        var result = WSHInputBox(prompt, title, "New York");

        if (result != null)   
        {  // Cancel wasn't clicked, so get input.
            var intDoIt =  WshShell.Popup(result,
                                          0,
                                          "Result",
                                          vbOKOnly + vbInformation);
        }
        else
        { // Cancel button was clicked.
            var intDoIt =  WshShell.Popup("Sorry, no input",
                                          0,
                                          "Result",
                                          vbOKOnly + vbInformation);
        }                           

    ]]>
    </script>
</job>

and run it with either cscript.exe or wscript.exe. Alternatively, you could also use HTML Application (HTA) to create more elaborate GUIs.

Foad
  • 7,892
  • 8
  • 38
  • 126