views:

83

answers:

2

I still can not get my coding to run. When I try to run the coding, a blank page shows up. Specifically, what do I need to do in order to get this simple coding to function? All I need is someone to look this over or test it and tell me what I need to get my coding to work.

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

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

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


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

    // add one to the total number 
    classCtr++;
    gpatotalvalue = gpatotalvalue + parseFloat(myClass[classCtr][1]);
    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("<tr>");
    document.write("<td>Class Name</td><td align='center'>Class Grade</td><td align='center'>Grade Credit</td>");
    document.write("</tr>");

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

     document.write("</td>");


     // increment the counter
   clsGrade++;
  }

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


 //-->
</script>
</font></body>
</html>
A: 

In this line you used class instead of myClass:

myClass[classCtr][2] = {"A":4,"B":3,"C":2,"D":1,"F":0}[myClass[classCtr][1]];

In these lines you were increasing classCtr first, which meant that you get an undefined value as you are accessing an element beyond the end of the array:

gpatotalvalue = gpatotalvalue + parseFloat(myClass[classCtr][1]);
classCtr++;
Guffa
As pointed out in [an answer](http://stackoverflow.com/questions/3227740/javascript-is-not-working#3227788) to the earlier version of this question, `classCtr` isn't actually initialized. In addition to changing the order of those lines, `classCtr` needs to be set to an initial value before it is first used.
Phil Ross
+1  A: 

Heres a working version, although I'm not sure if the GPA calculation is correct, don't have GPA's in New Zealand so not sure how its calculated. I added some comments to the code it point out where you had gone wrong :)

<html>
<body>
<script language="JavaScript">
<!--
var classCtr = 0; // should be initialised to 0 so it can be incremented
var nmAnswer;
var clsGrade;
var totalvalue;
var gpatotalvalue = 0;
var totalvalue = 0;
// set up one dimensional array
var myClass = new Array();

//removed some unused variables

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

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


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

    gpatotalvalue = gpatotalvalue + parseFloat(myClass[classCtr][2][ myClass[classCtr][1] ] ); //moved this line above the classCtr++; line so you weren't accessing an undefined variable, there some changes here to access the myClass[classCtr][2] object value correctly..

    // add one to the total number 
    classCtr++;

    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("<tr>");
    document.write("<td>Class Name</td><td align='center'>Class Grade</td><td align='center'>Grade Credit</td>");
    document.write("</tr>");

// Loop through array displaying html and javascript values in the array
while (myClass[clsGrade]) //the previous conditional would only out put the first "persons" grades so changed this to loop through until all peoples grades
  {
     document.write("<tr>");
     document.write("<td>");
     document.write (myClass[clsGrade][0]); //had class instead of myClass
     document.write("</td>");
     document.write("<td align='center'>");
     document.write (myClass[clsGrade][1]);//had class instead of myClass
     document.write("</td>");
     document.write("<td align='center'>");
     document.write (myClass[clsGrade][3]);//had class instead of myClass
     document.write("</td>");
     document.write("</tr>");

     document.write("</td>");


     // increment the counter
    clsGrade++;
  }

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


 //-->
</script>
</font></body>
</html>
RueTheWhirled
Oh my goodness, I could hug you! THANK YOU THANK YOU THANK YOU! This is so kind of you to do, you don't understand how much I deeply appreciate this! It works and with your comments I understand why. You are amazing! Many thanks!
kHuynh
Although putting the entire array of grades in each item makes no sense at all. You removed the code that picks out a single grade from the array.
Guffa
Ahh yeah true, yeah I didn't want to spend to long with the code, its pretty knarly.
RueTheWhirled