Trying to find the binary of 3 items in array. Getting infinite loop

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);

1 thought on “Trying to find the binary of 3 items in array. Getting infinite loop”

Leave a Comment