10

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.

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 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. Commented Jan 21, 2017 at 9:22

4 Answers 4

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
Sign up to request clarification or add additional context in comments.

1 Comment

Node.js console.log() is surprisingly rich. Unpacks and colorizes objects. And Unicode works! The scary prompt during Node.js install about compiling C/C++ appears to be optional. Basic stuff works without checking that box. Wish I knew what is missing though...
4

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.

Comments

2

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.