GPA Calculator

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

GPA Calculator

Post by SolarFloss on Wed Sep 18, 2013 2:55 pm
([msg=77407]see GPA Calculator[/msg])

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? :D

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();




Please and Thankyou.
SolarFloss
New User
New User
 
Posts: 6
Joined: Mon Sep 02, 2013 4:41 pm
Blog: View Blog (0)


Re: GPA Calculator

Post by -Ninjex- on Wed Sep 18, 2013 5:23 pm
([msg=77409]see Re: GPA Calculator[/msg])

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.
If you're not willing to learn, no one can help you. If you're determined to learn, no one can stop you.⠠⠵
The absence of evidence is not evidence of absence.
I can explain it for you, but I can't understand it for you.
User avatar
-Ninjex-
Addict
Addict
 
Posts: 1302
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)


Re: GPA Calculator

Post by SolarFloss on Thu Sep 19, 2013 5:36 am
([msg=77415]see Re: GPA Calculator[/msg])

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
New User
 
Posts: 6
Joined: Mon Sep 02, 2013 4:41 pm
Blog: View Blog (0)


Re: GPA Calculator

Post by -Ninjex- on Thu Sep 19, 2013 6:04 am
([msg=77416]see Re: GPA Calculator[/msg])

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
If you're not willing to learn, no one can help you. If you're determined to learn, no one can stop you.⠠⠵
The absence of evidence is not evidence of absence.
I can explain it for you, but I can't understand it for you.
User avatar
-Ninjex-
Addict
Addict
 
Posts: 1302
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)


Re: GPA Calculator

Post by SolarFloss on Thu Sep 19, 2013 2:20 pm
([msg=77420]see Re: GPA Calculator[/msg])

-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
New User
 
Posts: 6
Joined: Mon Sep 02, 2013 4:41 pm
Blog: View Blog (0)


Re: GPA Calculator

Post by 0phidian on Thu Sep 19, 2013 4:28 pm
([msg=77421]see Re: GPA Calculator[/msg])

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?
User avatar
0phidian
Poster
Poster
 
Posts: 269
Joined: Sat Jun 16, 2012 7:04 pm
Blog: View Blog (0)


Re: GPA Calculator

Post by SolarFloss on Thu Sep 19, 2013 8:35 pm
([msg=77426]see Re: GPA Calculator[/msg])

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
New User
 
Posts: 6
Joined: Mon Sep 02, 2013 4:41 pm
Blog: View Blog (0)


Re: GPA Calculator

Post by tgoe on Thu Sep 19, 2013 8:55 pm
([msg=77427]see Re: GPA Calculator[/msg])

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.
User avatar
tgoe
Contributor
Contributor
 
Posts: 639
Joined: Sun Sep 28, 2008 2:33 pm
Location: q3dm7
Blog: View Blog (0)



Return to Interpreted Languages

Who is online

Users browsing this forum: No registered users and 0 guests