Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

29
  • 14
    Here is the explanation Commented Jul 26, 2013 at 14:44
  • 2
    toString(2) doesn't work because you are getting the input from text. Use this: function decToBase(dec, base){ return parseInt(dec).toString(base); } alert(decToBase(dec, 2)); Commented May 29, 2015 at 13:47
  • 1
    You are assuming that the input is text, but the function in the answer expect a integer... So, if input is a text just convert it to integer, use the fake bitshift and it's done Commented May 30, 2015 at 18:30
  • 1
    @barlop when you get the value from any input its type defaults to text, that's what I assumed the other guy is doing, probably getting it's input from the user which would give you a, for example, "10". That's why I suggested the parseInt, the bitwise operation suggested works because it uses a specific operator(>>>) that results into a integer, if he did it with the + operator then it would concatenate a "0" because the first number type would be a string. parsetInt() is more readable, just that. Commented Jun 3, 2015 at 23:42
  • 1
    For what it's worth, this coding exercise failed on a test for 5934405041, whereas Manatok's answer with the ~ operator used on negative numbers did the trick. codewars.com/kata/526571aae218b8ee490006f4/train/javascript Commented Jan 15, 2025 at 19:27