2

I have an array, gradesArray, which I'm able to see with console.log(gradesArray) within my program, but when I go to access it typing gradesArray or gradesArray.length or something similar into the (Chrome) console, I get ReferenceError: gradesArray is not defined.

Here's the jQuery I use to fill the array with Grade objects:

$(document).on('click', '#savegrades', saveGrade);

function saveGrade() {                     // grabbing data from a form
    var studentClass = currentClass;
    var studentName = currentStudent;
    var dateStamp = $(this).parents('#gradingform').children('#date').val().replace(/-/g, "");
    var teamwork = parseInt( $(this).parents('#gradingform').children('#teamwork').val() );
    var participation = parseInt( $(this).parents('#gradingform').children('#participation').val() );
    var punctuality = parseInt( $(this).parents('#gradingform').children('#punctuality').val() );
    var professionalism = parseInt( $(this).parents('#gradingform').children('#professionalism').val() );
    var timemanagement = parseInt( $(this).parents('#gradingform').children('#timemanagement').val() );

    var newGrade = new Grade(studentClass, studentName, dateStamp, teamwork, participation,         // create new Grade object from form values
                                punctuality, professionalism, timemanagement);

    alert('class: ' + newGrade.studentClass + ', name: ' + newGrade.studentName + ', date: ' + newGrade.dateStamp + ', teamwork: ' + newGrade.teamwork + ', participation: ' + newGrade.participation + ', punctuality: ' + newGrade.punctuality + ', professionalism: ' + newGrade.professionalism + ', time management: ' + newGrade.timemanagement + ', total: ' + newGrade.total);

    gradesArray.push(newGrade);
    console.log(gradesArray);
}

After this code runs, that console.log(gradesArray) bit works just fine, giving me a nice array of Grade objects. But when I type gradesArray into the console, I get a reference error telling me that the variable is undefined. Also, later in the program, I try to use if (typeof gradesArray != 'undefined'), but the function doesn't fire.

What's the deal? The browser will log the array if I ask it to from the .js file, but it won't if I ask it to right from the console; and, even after I've filled it with objects, it's still undefined, even though they're showing up in the console.

Thanks in advance.

7
  • you forgot brackets saveGrade() Commented Jul 30, 2013 at 23:26
  • 3
    Where is your gradesArray originally declared? Commented Jul 30, 2013 at 23:27
  • 3
    @TimurShahbanov: Wrong. Commented Jul 30, 2013 at 23:29
  • I declared the array at the very beginning of the document ready function. Commented Jul 30, 2013 at 23:29
  • Problem solved--just forgot to make it global by declaring the array outside of the $(document).ready(function() {...});. Must be tired. Thanks to all. Commented Jul 30, 2013 at 23:33

2 Answers 2

4

You did not copy all code; your code is wrapped in another function ($(function () {})?) that contains the var statement for gradesArray. Because it is not a global variable, it cannot be inspected from the console. Easiest is to put the var statement outside your $():

var gradesArray = []
$(function () { ... });
Sign up to request clarification or add additional context in comments.

2 Comments

Yes! Solved. It's wrapped in the document ready function. Good call.
Of course it was wrapped into $(...), so it's not available to console, +1 ^^
0

By the time you log in the console, gradesArray is out of scope. Try declaring it in global scope if you need it there.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.