## GPA Calculator

For the discussion of Perl, Python, Ruby, and PHP and other interpreted languages.

### GPA Calculator

Okay, so I just started to learn Javascript about a month ago. And I think I am somewhat good at it now. I haven't really learned much HTML, so I haven't put it in a web page or anything. I've been using an online compiler. My problem is that for my GPA calculator, if you get a whole number it will only display that number. I want it to show. "4.00" instead of just "4". Another problem is that when you get a long number it will show "4.415464536435" instead of "4.41". I have looked around for some ideas, but the ones that are given are a bit too complex for me...So can you help?

Code: Select all
`grades = [];numbers = [];     function getGrades(){gradenum = prompt("How many classes do you have?").toUpperCase();for (i = 1; i <= gradenum; i++){    grade = prompt("Insert grade #" + i + ":").toUpperCase();    grades.push(grade);}}getGrades()function getScores(){   for (i = 0; i < grades.length; i++)   {      switch(grades[i])        {            case "A":                grades[i] = 4.00;                numbers.push(grades[i]);            break;                            case "B":                grades[i] = 3.00;                numbers.push(grades[i]);            break;                            case "C":                grades[i] = 2.00;                numbers.push(grades[i]);            break;                            case "D":                grades[i] = 1.00;                numbers.push(grades[i]);            break;                            case "F":                grades[i] = 0.00;                numbers.push(grades[i]);            break;                         case "E":            alert("Sorry, this GPA calculator does not accept 'E's");            getGrades()         break;                  default:            alert("You did not input a valid grade");         break;        }   }}getScores()total = 0;function calculateGPA){      for(i = 0; i < numbers.length; i++)      {         total += numbers[i];         }     GPA = total/i;    alert("Your GPA: " + GPA);}calculateGPA();`

SolarFloss
New User

Posts: 6
Joined: Mon Sep 02, 2013 4:41 pm
Blog: View Blog (0)

### Re: GPA Calculator

Code: Select all
`function convert_int(num) {  x = num.toFixed(2);  return x;}`

You could add the above function on the number you wish to give two places, like so:

a = 1.98743;
b = convert_int(a);

This will set a variable b to 1.98

So after you add in the above function, have the bottom of your calulate gpa function look like so:

Code: Select all
`NewGPA = convert_int(GPA);alert("Your GPA: " + NewGPA);`

Also note that you will want to switch your regular integers back to a long integer value with multiple decimal places.
Spreading knowledge just once a day, can help keep the script kiddies away ⠠⠵
no_hope if world.map{|person, ic = 0| ic +=1 if ignorance.include?(person)}.compact.length > (world.length / 2)
The absence of evidence is not evidence of absence.

-Ninjex-

Posts: 1059
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)

### Re: GPA Calculator

Thanks so much! But I ran in to one problem. So I I made the function and everything, but it didn't work. So I just ended up doing this.

Code: Select all
`     GPA = total/i;    alert("Your GPA: " + GPA.toFixed(2));`

I just wanted to know if that would cause any problems if I had a bigger program?. Well problems other than having to type it over and over again.
SolarFloss
New User

Posts: 6
Joined: Mon Sep 02, 2013 4:41 pm
Blog: View Blog (0)

### Re: GPA Calculator

SolarFloss wrote:Thanks so much! But I ran in to one problem. So I I made the function and everything, but it didn't work. So I just ended up doing this.

Code: Select all
`     GPA = total/i;    alert("Your GPA: " + GPA.toFixed(2));`

I just wanted to know if that would cause any problems if I had a bigger program?. Well problems other than having to type it over and over again.

No problem, if you don't mind, could you post the error you had received? You may need to add var in front of the variable like so:

Code: Select all
`var NewGPA = convert_int(GPA);`

Also, it wouldn't cause any problems in a bigger program, it would just make typing it out tedious for you. Usually if you plan on using some block of code more than a few times, you want to try and wrap it into some sort of method. For instance, if you want to access every item in a array of number 1-10 and add 1 to each value; you could in fact access each item individually and add 1 to it, but it would be tedious to write something like:

array[0]+1;
array[1]+1;
array[2]+1;
array[3]+1;
etc
Spreading knowledge just once a day, can help keep the script kiddies away ⠠⠵
no_hope if world.map{|person, ic = 0| ic +=1 if ignorance.include?(person)}.compact.length > (world.length / 2)
The absence of evidence is not evidence of absence.

-Ninjex-

Posts: 1059
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)

### Re: GPA Calculator

-Ninjex- wrote:
No problem, if you don't mind, could you post the error you had received?

Sorry, but the compiler I use does not display the error message. It just wont work if there is an error. Which can get very annoying. What should I use to compile my code?

And yea, that's what I thought. But since it was a smaller program, I thought it wouldn't really matter. Thanks for the help.
SolarFloss
New User

Posts: 6
Joined: Mon Sep 02, 2013 4:41 pm
Blog: View Blog (0)

### Re: GPA Calculator

SolarFloss wrote:
Code: Select all
`     GPA = total/i;    alert("Your GPA: " + GPA.toFixed(2));`

I just wanted to know if that would cause any problems if I had a bigger program?. Well problems other than having to type it over and over again.

This should work fine. You are not actually changing the value of GPA, you are simply alerting what the function toFixed() returns, so it should not cause any problems. If you dont want to have to retype it then you can set it to a variable like so.
Code: Select all
`roundedGPA = GPA.toFixed(2);`

Sorry, but the compiler I use does not display the error message. It just wont work if there is an error. Which can get very annoying. What should I use to compile my code?

I've never heard of compiling javascript. What do you use?

0phidian
Poster

Posts: 241
Joined: Sat Jun 16, 2012 7:04 pm
Blog: View Blog (0)

### Re: GPA Calculator

0phidian wrote:I've never heard of compiling javascript. What do you use?

I'm not sure if I can call it a compiler. Idk what it REALLY is, but it's at: http://jsfiddle.net/

What's another way I can make and test my scripts?
SolarFloss
New User

Posts: 6
Joined: Mon Sep 02, 2013 4:41 pm
Blog: View Blog (0)

### Re: GPA Calculator

The qooxdoo playground is good. Click on the log button to see errors.
The qooxdoo library is a good way to make nice looking web apps without having to learn html & css.

tgoe
Contributor

Posts: 621
Joined: Sun Sep 28, 2008 2:33 pm
Location: q3dm7
Blog: View Blog (0)

### Who is online

Users browsing this forum: No registered users and 0 guests