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 |4 Answers
Reset to default 121) 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
num
argument will always beundefined
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:19bin_dec
is not accessible from outside). Plus you don't needeval
. – Margaret Bloom Commented Jan 21, 2017 at 9:22