问题
I wrote the following JavaScipt code which converts a binary number into a decimal number:
(function bin_dec(num) {
var x = num;
var result = 0;
for (var i = 0; i < x.length; i++) {
result += eval(x[x.length - i] * 2^i);
}
return result;
})()
I want to be able to run this code from the command-line. The filename is converter.js and I am running the command prompt window in the same directory as the file. I am trying to run this code using 01001100
as the function argument. Here are my attempts:
$ converter.js 01001100
and
$ converter.js -01001100
and
$ converter.js bin_dec(01001100)
But unfortunately, neither of these works. Can somebody please point out my mistake? Thanks in advance.
回答1:
1) Install Node.js if you haven't done so yet.
2) Change your converter.js
file like this:
function bin_dec(num) {
return parseInt(num, 2);
}
console.log(bin_dec(process.argv[2]));
3) Open a new command prompt in the folder where your script is and run
$ node converter.js 01001100
回答2:
In nodejs, assuming that you already have it installed since you are trying to run javascript from cmd, you will have to refer to the process.argv
array in order to get the arguments passed in the command line.
So your code will need to look like this:
(function bin_dec(num) {
var x = num;
var result = 0;
for (var i = 0; i < x.length; i++) {
result += eval(x[x.length - i] * 2^i);
}
return result;
})(process.argv[2])
Notice process.argv[2]
that is passed to the function. That will make the first argument available as the num
variable inside your function.
You may also add a console.log
somewhere if you want to have messages displayed on the screen, since the return statement will not print messages.
回答3:
Assuming you are running on Windows, You can call it like this:
(function bin_dec() {
var x = WScript.arguments(0);
var result = 0;
for (var i = 0; i < x.length; i++) {
result += eval(x[x.length - i] * 2^i);
}
return result;
})()
Where all parameters passed into the function are stored in WScript.arguments
.
However, this will not output the return
value to the command prompt, so you may want to test it out with this .js
file:
(function ShowAlert() {
var x = WScript.arguments(0);
WScript.ECho(x);
})()
See these links for more details:
MSDN - Arguments Property
SS64 - WScript Arguments
SS64 - VBScript Command line arguments
回答4:
You'll have to know that, few basic functions like console
or return
statements will not work in CLI.
If you're using windows, then use 'WScript.echo'
which is similar to console.log
and while executing the file make sure you do it this way like
Cscript.exe yourpath input_params
for example Cscript.exe converter.js 01001100
So your code should be
(function bin_dec() {
var x = WScript.arguments(0);
var result = 0;
for (var i = 0; i < x.length; i++) {
result += eval(x[x.length - i] * 2^i);
}
WScript.echo(result);
})();
and to run it should be
Cscript.exe converter.js 01001100
hope this helps you! For more information on CLI methods visit the following link
https://msdn.microsoft.com/en-us/library/2795740w(v=vs.84).aspx
来源:https://stackoverflow.com/questions/41777585/running-javascript-from-the-windows-command-prompt