How can I write a simple JScript input/output prog

2020-06-17 04:19发布

问题:

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?

回答1:

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.