1

Here is what I did. Used For loop to iterate through all elements., then find binary by recursion and return

var temp3 = [57,80,95];
var temp2 = [];
var temp4 = '';
var r;

for(i=0;i<temp3.length;i++) {
var  r = temp3[i];    
temp2.push(binary(r));
}
function binary(r) {
    console.log(r);
  if (r === 0) return r;
  temp3 = (r % 2) + temp3;
  binary(Math.floor(r / 2));
  return temp3;
}
//1100010
console.log(temp2);
2
  • what is binary doing? Commented Jan 31, 2021 at 14:55
  • Your function changes temp3 from the original array to a number. Also, there's no recursion. edit oh wait yes there is Commented Jan 31, 2021 at 14:55

2 Answers 2

1

You can use bitwise operators and recursion to easily convert a number to binary -

const binary = x => 
  x < 2
    ? String(x)
    : binary(x >> 1) + String(x & 1)

console.log(binary(57)) // 111001
console.log(binary(58)) // 111010
console.log(binary(80)) // 1010000
console.log(binary(95)) // 1011111
console.log(binary(1234567)) // 100101101011010000111
console.log(binary(12345678)) // 101111000110000101001110

If you want the result as a number instead of a string, we can do that too. This second technique has limitations though because JavaScript's numbers quickly overflow -

const binary = n => 
  n == 0
    ? 0 
    : 10 * binary (n >> 1) + (n & 1)

console.log(binary(57)) // 111001
console.log(binary(58)) // 111010
console.log(binary(80)) // 1010000
console.log(binary(95)) // 1011111
console.log(binary(1234567)) // 100101101011010000000    <- wrong
console.log(binary(12345678)) // 1.0111100011000008e+23  <- imprecise

Sign up to request clarification or add additional context in comments.

2 Comments

Nice as always. I think the second one might be simpler as const binary = (n) => n == 0 ? 0 : 10 * binary (n >> 1) + (n & 1)
i was looking at that earlier and thinking it was overly complex. couldn't see it. thanks Scott :D
0

The correct way to write the binary function would be this -

function binary(r, i = 0) {
    console.log(r);
    if (r === 0) return r;
    return (r % 2) * Math.pow(10, i) + binary(Math.floor(r / 2), i + 1);
}

Although, there are many ways to convert an int to binary, I feel this is one of the easiest ones. And, If you want further explanation or clarity please comment down below.

2 Comments

Hi Suraj, I'm actually a newbie. So, am not supposed to use any other inbuilt function. I really appreciate it if you can help me with more information. Thank you!
@jithinsurendran actually, this function is using the concept of default arguments or otherwise you would need to call the binary function with two arguments (like binary(r, 0) and the other thing is Math.pow(10, i) which actually raises 10 to the power of i. So you can actually remove both of these things but you would need to add further unnecessary code.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.