最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Running JavaScript from the windows command prompt - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question asked Jan 21, 2017 at 9:13 Wais KamalWais Kamal 6,1802 gold badges19 silver badges43 bronze badges 2
  • 3 you have an IIFE ... num argument will always be undefined because of how you've written the IIFE ... that aside ... running javascript on the command line ... how do you propose this to work? do you get any errors when you do that? I'm guessing you are running linux, right – Jaromanda X Commented Jan 21, 2017 at 9:19
  • 2 But.. have you any tool that parses and executes JS from the command line? Like NodeJS or similar? Besides that, you have an IIFE, so no function declaration is present (ie bin_dec is not accessible from outside). Plus you don't need eval. – Margaret Bloom Commented Jan 21, 2017 at 9:22
Add a comment  | 

4 Answers 4

Reset to default 12

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

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.

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

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

发布评论

评论列表(0)

  1. 暂无评论