Page 1 of 1

### GPA Calculator Posted: Wed Sep 18, 2013 2:55 pm
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();`

### Re: GPA Calculator Posted: Wed Sep 18, 2013 5:23 pm
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.

### Re: GPA Calculator Posted: Thu Sep 19, 2013 5:36 am
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.

### Re: GPA Calculator Posted: Thu Sep 19, 2013 6:04 am
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+1;
array+1;
array+1;
array+1;
etc

### Re: GPA Calculator Posted: Thu Sep 19, 2013 2:20 pm
-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.

### Re: GPA Calculator Posted: Thu Sep 19, 2013 4:28 pm
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?

### Re: GPA Calculator Posted: Thu Sep 19, 2013 8:35 pm
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?

### Re: GPA Calculator Posted: Thu Sep 19, 2013 8:55 pm
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.