title: Error while looping backwards through an array
body: <p>I started working on something and literally hit a roadblock early on, tried to think my way through the problem early on with my limited knowledge of javascript. </p>
  
  <p>I'm sure it's something simple and annoying, but i can't seem to see what is causing the error.</p>
  
  <p>Just working on a script to evenutally show data from a google spreadsheet using google scripts. But yeah, cannot seem to loop through specific values in reverse.</p>
  
  <p>Please see my annotated code below and thanks in advance.</p>
  
  <p>simple attempt to loop through an array forwards. 
  decided to go to basics as i'm stumpted. Works. Logger shows i going up by 1</p>
  
  <pre><code>function getInfo() {
  var sheet = SpreadsheetApp.openById("ID HERE").getSheetByName("Sheet1");
  var values = sheet.getDataRange().getValues();
  
  
  for(i=0;i &lt; values.length;i++){
  Logger.log([i]);
  
  }
  }
  </code></pre>
  
  <p>same as the above, except i'm trying to go through the array in reverse.
   works but so far we're only asking it what part of the loop we're in.</p>
  
  <pre><code>function getInfo() {
  var sheet = SpreadsheetApp.openById("ID HERE").getSheetByName("Sheet1");
  var values = sheet.getDataRange().getValues();
  
  
  for(i=values.length;i &gt; 0 ;i--){
  Logger.log([i]);
  
  }
  }
  </code></pre>
  
  <p>strap your pants on time, because we are asking the array for data, 
  looping forwards. It works. it returns the whole line of each entry in the array</p>
  
  <pre><code>function getInfo() {
  var sheet = SpreadsheetApp.openById("ID HERE").getSheetByName("Sheet1");
  var values = sheet.getDataRange().getValues();
  
  
  for(i=0;i &lt; values.length;i++){
  Logger.log([i]+" " + values[i]);
  
  }
  }
  </code></pre>
  
  <p>let's moonwalk it and try looping in reverse again.
  Cause i'm bad, i'm bad, heee heeeh, oww. It works :)</p>
  
  <pre><code>function getInfo() {
  var sheet = SpreadsheetApp.openById("ID HERE").getSheetByName("Sheet1");
  var values = sheet.getDataRange().getValues();
  
  
  for(i=values.length;i &gt; 0 ;i--){
  Logger.log([i]+ " " + values[i]);
  
  }
  }
  </code></pre>
  
  <p>After putting on my big boy pants, I was on the way to log a single entry in my array,</p>
  
  <p>looping through it forwards. Hurrah, pants remained clean and results were as expected.</p>
  
  <pre><code>function getInfo() {
  var sheet = SpreadsheetApp.openById("ID HERE").getSheetByName("Sheet1");
  var values = sheet.getDataRange().getValues();
  
  
  for(i=0;i &lt; values.length;i++){
  Logger.log([i]+" " + values[i][16]);
  
  }
  }
  </code></pre>
  
  <p>So why oh why doesn't the same work in reverse and always kickup an error 
  "(TypeError: Cannot read property "16" from undefined. (line 69, file "Code")Dismiss"
  Line 69 being ( <code>Logger.log([i]+ " " + values[i][16]);</code> ) in the code below.</p>
  
  <pre><code>function getInfo() {
  var sheet = SpreadsheetApp.openById("ID HERE").getSheetByName("Sheet1");
  var values = sheet.getDataRange().getValues();
  
  
  for(i=values.length;i &gt; 0 ;i--){
  Logger.log([i]+ " " + values[i][16]);
  
  }
  }
  </code></pre>