# GPA Calculator

Page 1 of 1

## 13 Replies - 15360 Views - Last Post: 04 December 2009 - 12:12 PM

### #1 p0oper89

Reputation: 0
• Posts: 3
• Joined: 06-May 08

# GPA Calculator

Posted 06 May 2008 - 05:19 PM

This program will allow you to calculate your GPA for one semester. After receiving an explanation of the program and entering the semester, the student will be allowed to enter as many courses as he/she sees fit. The program must then calculate and display the GPA earned for that semester.

In order to produce the GPA, several calculations are needed. For each course, the number of credits, multiplied by the grade point earned in that class, results in a value known as quality points. To calculate the GPA, the total number of quality points for the semester is divided by the total number of credits taken that semester, exclusive of credits for courses taken with the Pass/Fail grading method. Your program should NOT handle Pass/Fail courses at all. The only acceptable grade points are in the range of 0 through 4, where 4 is an A, 3 a B, 2 a C, 1 a D and 0 an F. So, for example, if you took one 4 credit course and received a B, your GPA for that semester would be:

4 credits * 3 points = 12 quality points
12 total quality points / 4 total credits = 3.000 GPA

If there is more than one course, you just keep adding the quality points to the total points and credits to the total credits.

To begin the program , you should print a message to the user explaining what the program does. Then you should ask the user to enter the semester. After these initial steps, you should begin asking the user to enter the number of credits and grade points for each course, one course at a time. The user should enter a -1 as the number of credits to end the input phase. So -1 is the sentinel value for this exercise. At this point a report should be generated that shows the total number of credits taken, the total quality points earned, and the GPA for the semester.

Your program must generate all of the same information as shown in the sample output. You do not need to match the output exactly, however the GPA should be shown to three decimal places. You may also use the data shown here to test your program for accuracy.

More Requirments and details:
* You are REQUIRED to use the following functions exactly as they are given:
o function PrintExplanation(){} - This function should display a description of the program to the user. It should let the user know the specific points values for each of the letter grades. You should print the explanation using an alert(). The function should not return anything.
o function GetValidInput(promptString, sentinel){} - This function takes two arguments: the string that contains the prompt you want to print and the sentinel, which is -1 in this program. You should ask the user for the number and then error check to make sure the number is either greater than 0 or equal to -1. As long as the number the user enters is less than or equal to 0 and not equal to the sentinel, it should continue to prompt for a new number. The function should return the valid number. The function should be general and not mention anything about credits.
o function GenerateGradeReport(semester, totalCredits, totalPoints){} - This function should display the grade report to the user. You should use document.write() to produce the report. You will make a call to the function CalculateGPA() from within this function. The function should not return anything.
o function CalculateGPA(totalCredits, totalPoints){} - This function simply performs the GPA calcuation. It should return the calculated GPA.
* You should use the GetValidInput() function to get the number of credits from the user.
* You do not have to error check the points. You can assume the user will only type 4, 3, 2, 1 or 0.
* Feel free to add any style you would like to the GPA Report.

Is This A Good Question/Topic? 0

## Replies To: GPA Calculator

### #2 PsychoCoder

Reputation: 1659
• Posts: 19,853
• Joined: 26-July 07

## Re: GPA Calculator

Posted 06 May 2008 - 06:05 PM

[rules][/rules]

### #3 p0oper89

Reputation: 0
• Posts: 3
• Joined: 06-May 08

## Re: GPA Calculator

Posted 07 May 2008 - 11:43 AM

PsychoCoder, on 6 May, 2008 - 06:05 PM, said:

[rules][/rules]

You useless. Obviously you do not know how to write this code. Go fuck ur self. Bye

### #4 BenignDesign

• holy shitin shishkebobs

Reputation: 7459
• Posts: 12,061
• Joined: 28-September 07

## Re: GPA Calculator

Posted 07 May 2008 - 12:14 PM

Well, he was a perky fellow, wasn't he?

### #5 Nykc

• Gentleman of Leisure

Reputation: 737
• Posts: 8,648
• Joined: 14-September 07

## Re: GPA Calculator

Posted 07 May 2008 - 01:10 PM

He double posted and is pissed no one will do it for him rofl.

### #6 PsychoCoder

Reputation: 1659
• Posts: 19,853
• Joined: 26-July 07

## Re: GPA Calculator

Posted 07 May 2008 - 01:26 PM

p0oper89, on 7 May, 2008 - 11:43 AM, said:

You useless. Obviously you do not know how to write this code. Go fuck ur self. Bye

Dude, I could write this in my sleep, but I'm not doing your work for you lazy ass

### #7 jennii

Reputation: 0
• Posts: 3
• Joined: 23-November 09

## Re: GPA Calculator

Posted 23 November 2009 - 09:06 PM

I actually have this same homework problem, and unlike the guy above I am trying to make it work. But I can't figure out why my variables keep coming back as undefined. Any advice? Here is what I have:

```		<script type="text/javascript">
<!--
/****************************************************************************/
/* This function displays an explanation of the the GPA calculator program. */
/* Input: Nothing 									  */
/* Output: Text / Expanation of program									 */
/****************************************************************************/

function PrintExplanation()
{
alert("This program will calculate your GPA for one semester. \n\n You will be asked to enter the number of credits and grade points earned for each course taken that was graded using the regular grading method. \n\n Please DO NOT enter Pass/Fail courses since they do not affect the GPA. \n\n Grade points are as follows: \n\n 4 for an A \n 3 for a B \n 2 for a C \n 1 for a D \n and 0 for an F");
}
/****************************************************************************/
/* This function gets all the information needed to calculate the users GPA */
/* Input: ??????															*/
/* Output: total credits and total quality points						   */
/****************************************************************************/

function GetValidInput()
{
while (credits != -1)
{
credits = prompt("Enter the number of credits for the course (-1 to quit) : ");
if (credits != -1)
{
points = prompt("Enter the number of points you received: \n\n 4 for A \n 3 for B \n 2 for C \n 1 for D \n and 0 for F");
quality = credits * points;
totalQuality = totalQuality + quality;
totalCredits = totalCredits + credits;
}
}
}

/*******************************************************/
/* This function displays the grade report to the user */
/* Input: semester, totalCredits, totalPoints		  */
/* Output: none										*/
/*******************************************************/

{
CalculateGPA();
document.write("Total Credits Taken: " + totalCredits + "<br/>Total Quality Points Earned: " + totalQuality + "<br/>GPA for " + semester + ": " + GPA);
}

/**********************************************************/
/* This function calculates the GPA from the data entered */
/* Inputs: totalCredits, totalPoints					  */
/* Outputs: GPA										   */
/**********************************************************/

function CalculateGPA()
{
GPA = totalQuality / totalCredits;
}
-->
</script>
<body>
<h1>GPA Calculator</h1>
</div>
<div id="body">
<script type="text/javascript">
<!--

var credits;			/* The amount of credits per class being entered */
var points;			/* The amount of points earned per class being entered */
var quality;			/* The amount of quality points earned per class */
var totalCredits;		/* The total amount of credits for the entire semester */
var totalQuality;		/* The total amount of quality points for the entire semester */
var GPA;			/* The final grade point average for the semester */

/* Initializations */
totalCredits = 0;
totalQuality = 0;

PrintExplanation();
GetValidInput();
-->
</script>
</div>
</body>

```

This post has been edited by jennii: 23 November 2009 - 09:07 PM

• MSX

Reputation: 131
• Posts: 722
• Joined: 28-April 07

## Re: GPA Calculator

Posted 24 November 2009 - 12:03 AM

Hi jennii,
Two things you have to take care about:
1- the function GenerateGradeReport (semester, totalCredits, totalQuality) accepts 3 arguments, and you're trying to write them on the document.
the problem is you're not passing those arguments when calling the function, so it will show undefined instead
but since semester, totalCredits, totalQuality are global vars, you don't need to pass them to the function and you can just remove the GenerateGradeReport() arguments, so the function will use the global vars not the local.
declare the function like this:
```function GenerateGradeReport ()

```

2- by default the prompt returns string and if you tried to sum the returned values it will be accumulated not summed, so you have to convert the returned values to a numbers like this:
```totalCredits = totalCredits + Number(credits);

```

notes:

to get the benefits of using functions, you can just return what a function calculates and use the function it self instead of storing its calculation in a new variable
example: the CalculateGPA()
instead of storing the result in GPA
```function CalculateGPA()
{
}

```

and the GenerateGradeReport() will be something like this:
```function GenerateGradeReport ()
{
document.write("Total Credits Taken: " + totalCredits + "<br/>Total Quality Points Earned: " + totalQuality + "<br/>GPA for " + semester + ": " + CalculateGPA());
}

```

hope it helps

### #9 cnampheonix

Reputation: 1
• Posts: 63
• Joined: 03-December 09

## Re: GPA Calculator

Posted 03 December 2009 - 01:22 PM

I also have the same homework, we went over parts of it in class, Im still really confused and Im really lost now as to what I did to my code.... Plz help, explanations in language for dumb ppl would be nice as well. Thank you
```<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<script type="text/javascript">

function PrintExplanation()
{
alert("This program will calculate your GPA for one semester. \n\nYou will be asked to enter the number of credits and grade points earned for each course taken that was graded using the regular grading method. \n\nPlease DO NOT enter Pass/Fail courses since they do not affect GPA. \n\nGrade points are: \n\n4 for A \n3 for B \n2 for C \n1 for D \nand 0 for F");
}

function GetValidInput(promptString, sentinel)
{
var input
do {
input = parseInt( prompt( promptString));
}while (input < 0 && input != sentinel)
return input;
}

{
var GPA = CalculateGPA(totalQuality, totalCredits);
document.write("Grade Report for ".fontsize(8) + semester.fontsize(8) + "<br/>Total Credits Taken: " + totalCredits + "<br/>Total Quality Points Earned: " + totalQuality + "<br/>GPA : " + GPA);
}

function CalculateGPA(totalQuality, totalCredits)
{

}
</script>
<body>
<h1>GPA Calculator</h1>
<script type="text/javascript">

/* Initializations */
var totalCredits = 0;
var totalQuality = 0;

PrintExplanation();

do {

credits = GetValidInput("How many credits?,-1 to quit",-1);
points = GetValidInput("How many points?, -1 to quit",-1);
totalCredits = totalCredits + credits;
totalQuality = totalQuality + points * credits;

}while(credits != -1)
totalCredits = totalCredits + 1;
totalQuality = totalQuality - (points * credits);

</script>
</body>

</html>

```

im guessing theres alot of problems with my variables
I want to make totalCredits and Quality global variables so the program can keep count, or is that the credits?
Not sure, but im trying to minimalize on as much global variables as possible.
I know this is bad but, my hw is due in couple hours T.T if i could get help with as much as possible, i check my email like every 30 mins. And yes I know its horrible to procrastinate!

edit*
Im also having trouble trying to quit the getvalidinput function (the whole -1 thing)

This post has been edited by cnampheonix: 03 December 2009 - 01:24 PM

• MSX

Reputation: 131
• Posts: 722
• Joined: 28-April 07

## Re: GPA Calculator

Posted 03 December 2009 - 04:03 PM

hi,
see if this can help.
```<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<script type="text/javascript">

function PrintExplanation()
{
alert("This program will calculate your GPA for one semester. \n\nYou will be asked to enter the number of credits and grade points earned for each course taken that was graded using the regular grading method. \n\nPlease DO NOT enter Pass/Fail courses since they do not affect GPA. \n\nGrade points are: \n\n4 for A \n3 for B \n2 for C \n1 for D \nand 0 for F");
}

function GetValidInput(promptString, sentinel)
{
var input

input = parseInt( prompt( promptString));
if(isNaN(input)){
input = GetValidInput(promptString, sentinel)
}

return input;
}

{
var GPA = CalculateGPA(totalQuality, totalCredits);
document.write("Grade Report for ".fontsize(8) + semester.fontsize(8) + "<br/>Total Credits Taken: " + totalCredits + "<br/>Total Quality Points Earned: " + totalQuality + "<br/>GPA : " + GPA);
}

function CalculateGPA(totalQuality, totalCredits)
{

}
</script>
<body>
<h1>GPA Calculator</h1>
<script type="text/javascript">

/* Initializations */
var totalCredits = 0;
var totalQuality = 0;
var credits=0;

PrintExplanation();

while(credits != -1){

credits = GetValidInput("How many credits?,-1 to quit",-1);
if (credits==-1)break;
points = GetValidInput("How many points?, -1 to quit",-1);
if (points==-1)break;

totalCredits = totalCredits + credits;
totalQuality = totalQuality + points * credits;

}

</script>
</body>

</html>

```

### #11 cnampheonix

Reputation: 1
• Posts: 63
• Joined: 03-December 09

## Re: GPA Calculator

Posted 03 December 2009 - 04:59 PM

Thanks! ill check it out now

### #12 cnampheonix

Reputation: 1
• Posts: 63
• Joined: 03-December 09

## Re: GPA Calculator

Posted 03 December 2009 - 05:12 PM

I tinkered with the code a lil bit and heres what I got so far
I made it so that it would error check if the number was less than -1
```<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<script type="text/javascript">

function PrintExplanation()
{
alert("This program will calculate your GPA for one semester. \n\nYou will be asked to enter the number of credits and grade points earned for each course taken that was graded using the regular grading method. \n\nPlease DO NOT enter Pass/Fail courses since they do not affect GPA. \n\nGrade points are: \n\n4 for A \n3 for B \n2 for C \n1 for D \nand 0 for F");
}

function GetValidInput(promptString, sentinel)
{
var input

input = parseInt( prompt( promptString));
if((input) < -1){
input = GetValidInput(promptString, sentinel)
}

return input;
}

{
var GPA = CalculateGPA(totalQuality, totalCredits);
document.write("Grade Report for ".fontsize(8) + semester.fontsize(8) + "<br/>Total Credits Taken: " + totalCredits + "<br/>Total Quality Points Earned: " + totalQuality + "<br/>GPA : " + GPA);
}

function CalculateGPA(totalQuality, totalCredits)
{
return (totalQuality / totalCredits).toFixed(2);

}
</script>
<body>
<h1>GPA Calculator</h1>
<script type="text/javascript">

/* Initializations */
var totalCredits = 0;
var totalQuality = 0;
var credits=0;

PrintExplanation();

while(credits != -1){

credits = GetValidInput("How many credits?,-1 to quit",-1);
if (credits==-1)break;
points = GetValidInput("How many points?, -1 to quit",-1);
if (points==-1)break;

totalCredits = totalCredits + credits;
totalQuality = totalQuality + points * credits;

}

</script>
</body>

</html>

```

I mite change a couple things but I think it looks good so far

• MSX

Reputation: 131
• Posts: 722
• Joined: 28-April 07

## Re: GPA Calculator

Posted 04 December 2009 - 03:39 AM

Hi again,
that's good, but the only reason to add the checking for NaN is to prevent the user from entering a text instead of number
so, what if you combine these two checking in one line
```if(input < -1 || isNaN(input))

```

aside note: you can also edit the GetValidInput() function to check if the entered grade points is greater than 4, maybe you need here to add a new argument to the function in order to check the valid range that the user can enter..

### #14 jennii

Reputation: 0
• Posts: 3
• Joined: 23-November 09

## Re: GPA Calculator

Posted 04 December 2009 - 12:12 PM

Thank you soooo much ahmad! Its like I had a total brain fart about the parts where it's reading in the number as a string. I had forgotten that the teacher had been having us read in the values using parseInt() after prompting for them:

```credits = prompt("Enter the number of credits for the course (-1 to quit) : ");
credits = parseInt (credits);

```

its a little more lengthy, but since it is the way he has been having us do it, I will do it that way. And it never occured to me to use the CalculateGPA() like that! Thank you so much! You were a lifesaver!

This post has been edited by jennii: 04 December 2009 - 12:13 PM