title: Javascript: TypeError variable is undefined
body: <p>I am currently building a small web application with similar functionality across all modules. I want to code small generic functions so that all programmers next to me, call these functions and these functions return necessary but important data for them to implement their functionality. In this example, I am trying to deal with the typical "choose true or false" exercise. So from the template.php they call this function:</p>
  
  <pre><code>function checkAnswers(){
  var radiobuttons = document.form1.exer1;
  var correctAnswers = answers(); //this is an array of string
  var checkedAnswers = checkExerciseRB(radiobuttons, 2, correctAnswers);
      for(i=0; i&lt;checkedAnswers.length; i++){
          alert(checkedAnswers[i]);
      }
  }
  </code></pre>
  
  <p>Function checkExerciseRB is my generic function, it is called from checkAnswers. </p>
  
  <pre><code>function checkExerciseRB(rbuttons, opciones, correct){
      var answers = new Array();
      var control = 0;
      for(i=0; i&lt;rbuttons.length; i++){
          var noPick="true";
          for(j=0; j&lt;opciones; j++){
              if(rbuttons[control+j].checked){
                  if(rbuttons[control+j].value==correct[i]){
                      answers[i]= 1;
                      noPick="false";
                      break;
                  }
                  else{
                      answers[i]=2;
                      noPick="false";
                      break;
                  }
              }
          }
          if(noPick=="true")
              answers[i]=0;
          control=control+opciones;
      }
      return answers;
  }
  </code></pre>
  
  <p>It works great but while looking at my favorite browsers (FireFox, Chrome) error log it says:</p>
  
  <pre><code>TypeError: rbuttons[control + j] is undefined
  </code></pre>
  
  <p>Any clue on how to deal with this matter?</p>