-2

I would like to ask help whether am I doing the right thing or not. You see I am trying to test myself by displaying the bit pattern of a number in the most efficient way as possible. But I'm having trouble on how to display the pattern cause I'm still learning javascript. Here's my code.

<script>
    var bitPattern = function(given) {
        for(var i = 1 << 31; i > 0; i = i / 2){
            document.write((given & i) ? 1 : 0);
        }
    };

    var number = prompt("Enter a number to convert: ");

    bitPattern(number);
</script>
Armand
  • 23,463
  • 20
  • 90
  • 119
Kael
  • 391
  • 1
  • 10
  • 21

1 Answers1

3

The best way to do this is:

var number = prompt("Enter a number to convert: ");
var bitPattern = parseInt(number).toString(2);

document.write(bitPattern);
drinovc
  • 521
  • 5
  • 16