Monday, May 20, 2024
141
rated 0 times [  148] [ 7]  / answers: 1 / hits: 41831  / 7 Years ago, sat, january 21, 2017, 12:00:00

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.


More From » command-line

 Answers
11

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

[#59262] Thursday, January 19, 2017, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
freddiej

Total Points: 294
Total Questions: 95
Total Answers: 97

Location: Saudi Arabia
Member since Sat, Aug 20, 2022
2 Years ago
;