Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running JavaScript from the windows command prompt

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.

like image 831
Wais Kamal Avatar asked Jan 21 '17 09:01

Wais Kamal


People also ask

Can I run JavaScript from command line?

You can run JavaScript console in terminal or any command-line interface using Node. js, an open-source, platform-agnostic runtime that executes JavaScript outside a web browser.

How do I run JavaScript locally?

The easiest way to do this on Windows is to hold down the SHIFT key and then right click, then choose Open command window here. From the command window, type the command node and your command prompt will be transformed in to an interactive JavaScript REPL.

Can JavaScript run natively on Windows?

Then once you have NodeJS installed under your system, you can run any . js code natively by typing `node my_script. js" in your cmd window. NodeJS for sure will give you access to the file system.


3 Answers

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
like image 89
GOTO 0 Avatar answered Oct 13 '22 23:10

GOTO 0


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.

like image 40
Ibrahim Avatar answered Oct 13 '22 23:10

Ibrahim


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

like image 44
Bassie Avatar answered Oct 14 '22 00:10

Bassie