[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now


So here is a interesting question

Posted on 2014-01-19
Medium Priority
Last Modified: 2014-01-21
So did a interview and the question was without using any native browser functions convert a decimal to binary format in JavaScript.   I didn't know that off hand.
Question by:stargateatlantis
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 82

Accepted Solution

leakim971 earned 1000 total points
ID: 39793275
Test page : http://jsfiddle.net/uLH2B/
var d = 8;
var b = "";
while(d>0) {
    b = d%2 + b;
    d = d>>1;
alert( b );

Open in new window

LVL 84

Expert Comment

by:Dave Baldwin
ID: 39793278
Since javascript is weakly typed like PHP is, I usually just add 0 or mulitply by 1.  But then... javascript doesn't actually have a 'decimal' format.  It's strings or numbers with automatic provisions for int or float.

LVL 43

Assisted Solution

Rob earned 1000 total points
ID: 39793445
This converts the decimal part as well:


var n = "8.75";
var a = n.split(".");
var i = a[0]*1;
var den = 1;
for (j=0;j<a[1].length;j++) {
  den = den * 10;
var d = a[1]/den;

var b1 = "";
var b2 = "";

while(i>0) {
  b1 = i%2 + b1;
  i = i>>1;

var tmp = 0;
while(d>0) {
  tmp = ("" + d*2 + "").split(".");
  b2 = b2 + tmp[0];
  d = tmp[1] / 10;
console.log( b1 + "." + b2 );

Open in new window


Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question