title: what should be the correct Algorithm to Get Array B from Array A
body: <p>Array A= [1,0,1,1,1,1,0,1,1]];
  And the result will be B B=[1,0,4,3,2,1,0,2,1];
  the elements of A is given and the result will be B.
  Elements in A can only be 0 and 1. 
  we need to count backward from the last element in A, if there are consecutive 1's then the element in A and B will be 1 but for the 2nd consecutive 1 in A it will be 2 in be and for the 3rd one in A the element will be 3 in B
  but for 0 it will be 0. I have tried the following way but I am not getting the output.</p>
  
  <pre><code>&lt;script&gt;
  
  var A= [1,0,1,1,1,1,0,1,1]];
  var B =[];
  //var B= A.length;
  
  var lastValueOne;
  var consecutiveOnes = 0;
  
  for (i = A.Length - 1; i &gt;= 0; i--)
      {
          if ( lastValueOne== false) 
          {
                  consecutiveOnes = 0; 
                  B[i] = 0;
          }
          else 
          { 
              consecutiveOnes++; 
              B[i] = consecutiveOnes; 
          }
  
          if (A[i] == 1) 
          { 
              lastValueOne = true; 
          }
  
          else 
          { 
              lastValueOne = false; 
          }
      }
  
    //console.log(B[i]);
   document.getElementById("demo").innerHTML = B;
  </code></pre>