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.
numargument will always beundefinedbecause 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, rightbin_decis not accessible from outside). Plus you don't needeval.