4
\$\begingroup\$

I'm trying it out just to see if I could solve it. It would be great if I could receive tips on how to improve it.

//input
var decimal = prompt("Please enter a decimal number");
if (decimal >= 1 && decimal < 1000000000) {
    //convert to binary
    var binConvert = parseInt(decimal, 10).toString(2);

    //reverse binary number
    var makeString = binConvert.toString();
    var srj = makeString.split("").reverse().join("");
    var makeNumber = Number(srj);

    //convert back to decimal
    alert(parseInt(makeNumber, 2));

} else {
    alert("Input number should be: 1 <= N < 1 000 000 000. Please try again.");
}

jsFiddle

\$\endgroup\$

2 Answers 2

4
\$\begingroup\$
prompt("Please enter a decimal number");

is a little confusing to me. I thought you wanted a number with a few decimal digits on it.

It might make more sense to say "Please enter a base 10 number"


You are a little incosistent in how you turn your string numbers into number numbers.

In the beginning, you use parseInt:

var binConvert = parseInt(decimal, 10).toString(2);

In the end, you use parseInt:

alert(parseInt(makeNumber, 2));

But in the middle, you use Number:

var makeNumber = Number(srj);

It is better to be consistent and stick to one function (in my opinion, you should use parseInt).


If you take the above advice in using parseInt, you can then get rid of the last parseInt call like so:

var makeNumber = parseInt(srj, 2); // I moved the parseInt call up here

//convert back to decimal
alert(makeNumber);

This might just be me, but your comments are a little confusing.

Here, you wrote:

//convert back to decimal
alert(parseInt(makeNumber, 2));

Aren't you technically converting to binary? (The radix parameter of parseInt is 2).

\$\endgroup\$
2
  • \$\begingroup\$ He has it right. It's just that the javascript API names make it a little confusing. parseInt(x,2) takes a string, assumes it is base 2 (binary) and converts it to a number. No further toString() is necessary to print that number as decimal because that's what js does by default \$\endgroup\$
    – slebetman
    May 3, 2015 at 23:18
  • \$\begingroup\$ Thanks for the great feedback, you guys! I will look into it and learn from it! \$\endgroup\$
    – Felix
    May 4, 2015 at 23:19
4
\$\begingroup\$

SirPython already pointed out what I would have, so this is a wholly different approach rather than a review.

Instead of doing number-to-string-to-number conversions, we can employ some bitwise voodoo:

var input = prompt("Please enter a positive decimal number"),
    decimal = parseInt(input, 10);

if(decimal > 0 && decimal < 1000000000) {
  
  var reversed = decimal & 1; // set the least-significant bit (LSB)
  
  // loop through remaining bits
  while(decimal > 1) {
    reversed <<= 1; // shift a zero bit onto "the end" of reversed
    decimal  >>= 1; // shift the LSB off of decimal
    reversed += decimal & 1; // add decimal's new LSB
  }
  
  alert(reversed);

} else {
  alert("Input number should be: 0 < N < 1 000 000 000.");
}

Basically, each bit we shift off of decimal gets shifted onto reversed in, well, in reverse.

By the way: It can only handle input up to 2,147,483,647 (i.e. signed 32-bit integer max). Bitwise operations in JavaScript start misbehaving after that. I've just kept your current limit, though.

\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.