title: TypeError Cannot read property of undefined
body: <p>Didn't exactly know what to title this question so I just named it the error I'm getting in console. The full error is:</p>
  
  <blockquote>
    <p>Uncaught TypeError: Cannot read property 'replaceAt' of undefined</p>
  </blockquote>
  
  <p>The problem with this is that I can see what the function accessing the <code>replaceAt</code> function is trying to do but do not know why. I have checks in place to stop this from happening but it still happens.</p>
  
  <p>Basically I'm writing a text-based game in which a character and an AI battle on a 2D map (not an Array map). It happens when the AI tries to move off the map (only out the top and bottom that I've noticed) but I don't understand why.</p>
  
  <p>The <code>replaceAt</code> function I am using is:</p>
  
  <pre><code>String.prototype.replaceAt = function(index, char) {
      return this.substr(0, index) + char + this.substr(index+char.length);
  }
  </code></pre>
  
  <p>I am using this to edit strings in an array easily. I would also post the AI function however it is 447 lines long and don't think it should be posted here (if you think otherwise I'll edit it in).</p>
  
  <p>My game can be found here: <a href="http://jsfiddle.net/spedwards/R4F8L/" rel="nofollow">http://jsfiddle.net/spedwards/R4F8L/</a></p>
  
  <p><strong>Why is it that I get the TypeError when I have already got checks in stopping impossible actions?</strong></p>