Take the 2-minute tour ×

My coding for JavaScript is not working and I can't figure out why. Can someone look it over and tell me what I am overlooking?

This is for my JavaScript assignment the link below is the assignment. Every time I have attempted to run the coding, it opens up to a blank page. But I can't see where my error(s) are. All I need is for someone to quickly read it and tell me what I can do to make it function.


<script language="JavaScript">
var classCtr;
var nmAnswer;
var clsGrade;
var totalvalue;
var gpatotalvalue;
gpatotalvalue = 0;
totalvalue = 0;
// set up one dimensional array
var class = new Array();
classnm = 0;

  // start columns in second dimension of the array
  class[classCtr] = new Array();

 // get values from user and put in array
 class[classCtr][0] = prompt ("Enter Class Name");
 class[classCtr][1] = prompt ("Enter grade recieved");
 class[classCtr][2] = {"A":4,"B":3,"C":2,"D":1,"F":0}[class[classCtr][1]];
 class[classCtr][3] = prompt ("Enter credit hours")

 // accumulate the total value
 totalvalue = totalvalue + parseFloat(class[classCtr][3]);

 // add one to the total number of cars
 totalvalue = totalvalue + parseFloat(class[classCtr][2]);
 nmAnswer = prompt ("Do you have more classes");
} while (nmAnswer == "yes");

  // set variable that is used as counter
 clsGrade = 0;

 // print out header for content
 document.write("<H2 align='center'>Grade Point Average</H2><br>");
 document.write("<table bgcolor='Grey' align='center' border='1' cellpadding='4' width='75%'>");
 document.write("<td>Class Name</td><td align='center'>Class Grade</td><td align='center'>Grade Credit</td>");

// Loop through array displaying html and javascript values in the array
while (clsGrade < classnm)
  document.write (class[classCtr][0]);
  document.write("<td align='center'>");
  document.write (class[classCtr][1]);
  document.write (class[classCtr][3]);


  // increment the counter

// finish the table of data and display the total value
document.write("<table bgcolor='grey' align='center' border='1' cellpadding='4' width='75%'>");
document.write("<td>Total value</td><td align='center'>" + totalvalue + "</td>");
document.write("<td>GPA</td><td align='center'>" + gpatotalvalue + "</td>");

share|improve this question
Could it be possible that classCtr not being initialized to anything is causing the error when initializing the second array within class[classCtr]? –  Marcin Jul 12 '10 at 15:38
add comment

3 Answers

classnm is never set to anything other than 0...

share|improve this answer
I did set classnm to 0 ...? –  kHuynh Jul 12 '10 at 10:49
Yeah, I think the author possibly changed the code to put the user values into the "class" Array and never changed the old logic. –  Andir Jul 12 '10 at 10:52
@kHuynh: Yeah, you set it to 0, but it never changes after that. classGrade == 0 and classNm == 0 so your while loop is never run because 0 is not less than 0. –  Andir Jul 12 '10 at 10:54
@kHuynh - yeah, you set it to 0, but then you enter a loop with this: while (clsGrade < classnm) which will not execute unless clsGrade is less than 0... –  cjk Jul 12 '10 at 10:55
Oh.. Well thank you –  kHuynh Jul 12 '10 at 11:00
add comment

class is a javascript reserved word, rename all "class" to something else. E.g. "myClass"

Renaming "class" should make your code work, but I still see many minimal errors in your code like unclosed "head" tag, use of uninitialized variable, etc. Suggest you review it thoroughly.

share|improve this answer
add comment

I ran your code in Firefox with the Firebug extension installed. It reported the following error.

class[classCtr] is undefined on Line 34

Line 34 is the following:


This error is occurring because you increment classCtr on the previous line, so the call to class[classCtr] attempts to access something that hasn't yet been defined.

Also, classCtr isn't assigned an initial value, so will have the value unassigned. The first set of values will therefore be stored in classCtr[undefined].

You'll need to initialize classCtr when it is declared and then increment it after you've finished handling each class.

share|improve this answer
Yet another reason to have everyone develop with firebug! I missed that in my first glance, but there's one problem. –  Andir Jul 12 '10 at 11:03
The problem lies before that line though: class[classCtr] = new Array(); is like saying class["undefined"] = new Array(); –  Andir Jul 12 '10 at 11:09
What do you suggest I should use instead in order to make it valid? –  kHuynh Jul 12 '10 at 11:12
add comment

Your Answer


