PDA

View Full Version : Help with arrays and functions

gabbagabbahey38
12-10-2010, 05:52 AM
Hi, new to this forum but have a quick question about arrays, I've been working on this all night, and I'm at a loss. I'm trying to calculate the gpa of a user by using arrays and functions. Everything seems to be working besides two things.

The array that contains the class names (className[]) always includes an extra two variables: my sentinel value which is finish, and "undefined"
The arrays at the end won't produce a number when multiplied together. creditData[] and qualityPoints[].

Also, if you have any suggestions on my code, I'd appreciate it.

<script type="text/javascript">
<!--
/****************************************************************
** PrintExplanation - prints a message containing instructions to the user without
** any variable needed.
** Inputs:None
** Outputs:None
****************************************************************/
function PrintExplanation()
{
}

/****************************************************************
** GetValidNumberInput - Error checks a number between two constraints.
** Inputs: promptString - allows the user to insert a string in order to be asked.
** lowerNum - the lower constraint you want to be compared.
** upperNum - the higher constraint you want to be compared.
** Outputs: numCredits
****************************************************************/

function GetValidNumberInput(promptString, lowerNum, upperNum)
{
var promptString;
var lowerNum;
var upperNum;
var numCredits;
numCredits=prompt(promptString);
while(numCredits<lowerNum || numCredits>upperNum)
{
numCredits=prompt(promptString);
}
numCredits=parseFloat(numCredits)
return numCredits
}

/****************************************************************
** CalculateGPA - Calculates the users GPA
** Inputs: totalCredits: The total amount of credits determined by the user.
** totalQualityPoints: The total amount of quality points determined by the user.
** Outputs: gpa - the users final grade point average.
****************************************************************/
function CalculateGPA(totalCredits, totalQualityPoints)
{
var totalCredits;
var totalQualityPoints;
var gpa;

gpa=totalQualityPoints/totalCredits
return gpa;
}

function GenerateGPAReport(totalCredits, totalQualityPoints)
{

}

/****************************************************************
** ConvertGradeToPoints - Converts a letter given by the user into a number corresponding to grade points.
** Inputs: grade - the letter of the grade the student recieved in the course.
** Outputs: points - the number value of the grade that was input by the user.
****************************************************************/

{
var points;
{
points=4
}
{
points=3
}
{
points=2
}
{
points=1
}
{
points=0
}
points=parseFloat(points);
return points;
}

//-->
</script>

<body>
<script type="text/javascript">

var promptString; /*Declares Variables*/
var className;
var numOfClasses;
var semester;
var numCredits;
var lowerNum=1;
var upperNum=4;
var class1;
var i=0;
var counter=0;
var totalCredits=0;
var totalPoints=0;
var counterCredits=0;
var points;
var gpa;
var qualityPoints;
var creditData;
var x=0;
var totalQualityPoints=0;

PrintExplanation();
var className = new Array(i);
semester=prompt("Please enter the semester you would like to calculate your GPA for.");
document.write("Grade report for " + semester + "<br />");

while(class1!="finish") /*Stores the names of the classes taken into an array*/
{
class1=prompt("Enter the Class name, or finish to end");
className[i]=class1
i++
}

while(counter<=i) /*writes the classes in the array from top to bottom*/
{
document.write(className[counter] + "<br />");
counter++
}

var creditData = new Array(i)
while(counterCredits<=i) /*counts the total number of credits for all the classes*/
{
numCredits=GetValidNumberInput("How many credits is " + className[counterCredits] + " class worth?",1,4)
creditData[counterCredits]=numCredits
totalCredits=totalCredits + numCredits
counterCredits++
totalCredits=parseFloat(totalCredits);
}

{
totalPoints=totalPoints+points
}

while(x<=i)
{
totalQualityPoints=totalQualityPoints + qualityPoints
x++
}

gpa=CalculateGPA(totalCredits, qualityPoints)
document.write(gpa);

</script>
</body>

Philip M
12-10-2010, 09:08 AM
It cannot work as you have a syntax error:-

The default of all your promts is undefined.
semester=prompt("Please enter the semester you would like to calculate your GPA for." , "" );

When you declare a variable with var you wipe out any previous instance of it:-

A prompt dialog can only return a single value., that is the value entered by the user.
numCredits=GetValidNumberInput("How many credits is " + className[counterCredits] + " class worth?",1,4)
Here you have assigned "1" as the default value. The correct way is something like:-
numCredits= prompt("How many credits is " + className[counterCredits] + " class worth?","");
You must check on the validity of that value in script:-
checkValidInput(numCredits); // function checkValidInputs checks for a number within a valid range.

You have declared your array with a length of i, which has a value of 0.
var className = new Array(i);

There are probably other errors as well.

You have omitted the ; at the line ends in many places. It is true that the ; is not mandatory, but if you make a habit of leaving it out you will run into trouble one day.
You should place the opening brace { on the same line as the if/else/while/do/function statement, not the next line. Again, you will run into trouble if you do not follow this advice (we had a case only the other day in this forum).