Binary Agent infinite loop

Binary Agent infinite loop
0.0 0

#1

I keep getting an infinite loop but I’m not seeing one, can somebody help me please, it is saying it is caused by the first for loop I have. Thank you.

function binaryAgent(num) {
  num=num.split(" ");
  var total=0;
  var final=[];
  
  
   for (i=0;i<num.length;i++)
   {
     total=0;
  var placeHolder=1;
 var str=num[i].split("");
  
  for (i=str.length-1;i>=0;i--)
    {
      if (str[i]==1) 
      {
       total+=placeHolder;
        
      } 
      else 
        {
          total+=0;
        }
      placeHolder*=2;
    }
      final.push(total);
   }
  
  
  return final;
}

binaryAgent("01000001 01000001");

#2

You’re overwriting the iterating variable, so it’s not counting as you expect it to. Relying on implicit variable declaration is never good and this is a good example why!


#3

The problem is since you are using the variable i in both for loops, you keep resetting the value of i. Try putting a different variable name (i.e. j) for the second for loop.


#4

Thank you very much!