-3

The javascript is supposed to convert the input into base2 and display it in the output. I'm confused at why the output"o" is the the exact same as the input"num". I don't understand why this won't work. I know that the data is being passed correctly because if I put simple math in the function, then the output is fine. However, when I try to put the conversion in, it will not work.

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="style.css">
    <title>Base Converter</title>
    <meta charset = "utf-8">
    <script>
        function Dec2Bin(val)
        {
            var num = val;
            var n = num.toString(2);
            convert.o.value = n;
        }
    </script>
</head>
<body>
    <center>
    <div class="banner"> <!--banner area-->
        Convert from base 10 to base 2
    </div>
    <div class="body"> <!--function area-->
        Please choose a number between 1 and 1,024

        <form name="convert" class="alignment">
            <input type="number" name="num">
            <input type="button" value="Convert" onclick="Dec2Bin(num.value)">
            <output name="o"></output>
        </form>
    </div>
</body>

Nathan Thompson
  • 77
  • 2
  • 11
  • 5
    `int` never changes, so it's going to be an infinite loop. Also, you can [change bases using `parseInt() and toString()`](http://stackoverflow.com/questions/1337419/how-do-you-convert-numbers-between-different-bases-in-javascript). – Spencer Wieczorek Jul 29 '15 at 02:56
  • 1
    `function ConvertToB2(int) {return int.toString(2)}` – zzzzBov Jul 29 '15 at 02:58
  • @SpencerWieczorek , I've used toString() and rather than the output display the number in a different base, it displays the same number that was put in. I've edited the code which is above. Any ideas what's going on? – Nathan Thompson Jul 30 '15 at 17:55
  • @NathanThompson Yes, it's because the value of `val` is a string, making `num` a string. `.toString(2)` needs to be done on an number so you just need to convert it to one. I've added that as an answer. – Spencer Wieczorek Jul 30 '15 at 18:28

2 Answers2

1

This is because the value of num is a string. So doing toString on a string isn't going to do anything. You simply need to change num to a number. You can do this by either doing var num = +val or var num = parseInt(val):

function Dec2Bin(val)
{
    var num = +val;
    var n = num.toString(2);
    convert.o.value = n;
}
        <form name="convert" class="alignment">
            <input type="number" name="num">
            <input type="button" value="Convert" onclick="Dec2Bin(num.value)">
            <output name="o" value=""></output>
        </form>


Note: Be careful on relying on your name items in your DOM to be global data. As it can cause problems with multiple items with the same name or other browsers. You may want to assign an id instead and use document.getElementById().

Spencer Wieczorek
  • 21,229
  • 7
  • 44
  • 54
  • Can't handle negative number – Aderemi Dayo May 16 '18 at 14:48
  • @AderemiDayo Yes, this is due to how `.toString()` was implemented on negatives. See [this question](https://stackoverflow.com/questions/16155592/negative-numbers-to-binary-string-in-javascript). For this question specifically the user is only dealing with positive integers, and is a correction to their approach. – Spencer Wieczorek May 16 '18 at 15:09
0

In your while loop, you loop while int > 0, but int never changes. Thus, it will loop forever (or 0 times).

Also, int is technically a reserved word in JavaScript, so you should avoid using it. Something like num would be appropriate.

Aaron Dufour
  • 17,288
  • 1
  • 47
  • 69