gabbagabbahey38
12-10-2010, 04: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()
{
alert("This program will calculate your Grade Point Average for any given semester. Please enter only graded classes, no Pass/Fail Classes.");
}
/****************************************************************
** 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.
****************************************************************/
function ConvertGradeToPoints(grade)
{
var grade;
var points;
if(grade == "A" || grade== "a")
{
points=4
}
else if(grade == "B" || grade == "b")
{
points=3
}
else if(grade == "C" || grade == "c")
{
points=2
}
else if(grade == "D" || grade == "d")
{
points=1
}
else if(grade == "F" || grade == "f")
{
points=0
}
points=parseFloat(points);
return points;
}
//-->
</script>
</head>
<body>
<script type="text/javascript">
var promptString; /*Declares Variables*/
var className;
var numOfClasses;
var grade;
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 grade;
var counterCredits=0;
var counterGrade=0;
var points;
var gpa;
var qualityPoints;
var creditData;
var gradeData;
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);
}
var gradeData = new Array(i) /*collects information about grades in an array*/
while(counterGrade<i=)
{
points=prompt("What grade did you recieve in " + className[counterGrade] + "?");
points=ConvertGradeToPoints(points)
gradeData[counterCredits]=points
totalPoints=totalPoints+points
counterGrade++
}
while(x<=i)
{
qualityPoints=creditData[x]*gradeData[x]
totalQualityPoints=totalQualityPoints + qualityPoints
x++
}
gpa=CalculateGPA(totalCredits, qualityPoints)
document.write(gpa);
</script>
</body>
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()
{
alert("This program will calculate your Grade Point Average for any given semester. Please enter only graded classes, no Pass/Fail Classes.");
}
/****************************************************************
** 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.
****************************************************************/
function ConvertGradeToPoints(grade)
{
var grade;
var points;
if(grade == "A" || grade== "a")
{
points=4
}
else if(grade == "B" || grade == "b")
{
points=3
}
else if(grade == "C" || grade == "c")
{
points=2
}
else if(grade == "D" || grade == "d")
{
points=1
}
else if(grade == "F" || grade == "f")
{
points=0
}
points=parseFloat(points);
return points;
}
//-->
</script>
</head>
<body>
<script type="text/javascript">
var promptString; /*Declares Variables*/
var className;
var numOfClasses;
var grade;
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 grade;
var counterCredits=0;
var counterGrade=0;
var points;
var gpa;
var qualityPoints;
var creditData;
var gradeData;
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);
}
var gradeData = new Array(i) /*collects information about grades in an array*/
while(counterGrade<i=)
{
points=prompt("What grade did you recieve in " + className[counterGrade] + "?");
points=ConvertGradeToPoints(points)
gradeData[counterCredits]=points
totalPoints=totalPoints+points
counterGrade++
}
while(x<=i)
{
qualityPoints=creditData[x]*gradeData[x]
totalQualityPoints=totalQualityPoints + qualityPoints
x++
}
gpa=CalculateGPA(totalCredits, qualityPoints)
document.write(gpa);
</script>
</body>