Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: TypeError variable is undefined

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:

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<checkedAnswers.length; i++){
        alert(checkedAnswers[i]);
    }
}

Function checkExerciseRB is my generic function, it is called from checkAnswers.

function checkExerciseRB(rbuttons, opciones, correct){
    var answers = new Array();
    var control = 0;
    for(i=0; i<rbuttons.length; i++){
        var noPick="true";
        for(j=0; j<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;
}

It works great but while looking at my favorite browsers (FireFox, Chrome) error log it says:

TypeError: rbuttons[control + j] is undefined

Any clue on how to deal with this matter?

like image 200
mrdeived Avatar asked Jul 20 '12 19:07

mrdeived


People also ask

Why is my JavaScript variable undefined?

A variable that has not been assigned a value is of type undefined . A method or statement also returns undefined if the variable that is being evaluated does not have an assigned value. A function returns undefined if a value was not returned .

How do you handle undefined and null in JavaScript?

The typeof operator for undefined value returns undefined . Hence, you can check the undefined value using typeof operator. Also, null values are checked using the === operator. Note: We cannot use the typeof operator for null as it returns object .

How do I check if JavaScript is undefined?

In a JavaScript program, the correct way to check if an object property is undefined is to use the typeof operator. If the value is not defined, typeof returns the 'undefined' string.


1 Answers

This probably means that control + j is greater than or equal to the length of the array rbuttons. There's no such array element as rbuttons[control + j].

You should learn how to use the JavaScript debugger in your favorite browsers! Debuggers are great. They let you watch this code run, line by line, as fast or as slow as you want, and watch how the value of control changes as you go.

You’ll watch it, and you’ll think “Oh! That line of code is wrong!”

like image 153
Jason Orendorff Avatar answered Oct 09 '22 00:10

Jason Orendorff