Clicky

I need to create a simple calculator in java:
- enter number grade in HTML
- will display letter grade in textbox on the html web

Here is what i have so far:


function process() {
    var NumberGrade = document.getElementById("enterGrade");
    var MyGrade = getmygrade(1);
    var Display = document.getElementById("letterGrade");
    Display.innerHTML = MyGrade;
}

function getmygrade(number) {
    var letterGrade = MyGrade(process);
    if (number <= 59)
        letterGrade = "F";
    else if (number >= 60 && number <= 69)
        letterGrade = "D";
    else if (number >= 70 && number <= 72)
        letterGrade = "C-";
    else if (number >= 73 && number <= 76)
        letterGrade = "C";
    else if (number >= 77 && number <= 79)
        letterGrade = "C+";
    else if (number >= 80 && number <= 82)
        letterGrade = "B-";
    else if (number >= 83 && number <= 86)
        letterGrade = "B";
    else if (number >= 87 && number <= 89)
        letterGrade = "B+";
    else if (number >= 90 && number <= 92)
        letterGrade = "A-";
    else if (number >= 93 && number <= 96)
        letterGrade = "A";
    else if (number >= 97 && number <= 100)
        letterGrade = "A+";


    return letterGrade
   
   
}

asked 11/26/2011 10:05

itconsultant1's gravatar image

itconsultant1 ♦♦


6 Answers:
You're saying Java, but I'm thinking you mean javascript (two different things), so I give the following examples in javascript showing three different methods for returning a letter grade from a number input.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Untitled Page</title>
</head>
<body>
<input type="text" id="enterGrade" />
<p id="letterGrade" style="font-size:2em"></p>
<input type="button" value="Get Letter Grade" onclick="process();" />
<script type="text/javascript">
function process() {
    var NumberGrade = document.getElementById("enterGrade");
    var MyGrade = getmygrade(NumberGrade.value) + " (from getmygrade())<br />";
    MyGrade += getmygrade_1(NumberGrade.value) + " (from getmygrade_1())<br />";
    MyGrade += getmygrade_2(NumberGrade.value) + " (from getmygrade_2())";
    var Display = document.getElementById("letterGrade");
    Display.innerHTML = MyGrade;
}

function getmygrade(number) {
    var letterGrade = "";
    switch(true){
        case (number <= 59):
            letterGrade = "F";
            break;
        case ((number >= 60) && (number <= 69)):
            letterGrade = "D";
            break;
        case ((number >= 70) && (number <= 72)):
            letterGrade = "C-";
            break;
        case ((number >= 73) && (number <= 76)):
            letterGrade = "C";
            break;
        case ((number >= 77) && (number <= 79)):
            letterGrade = "C+";
            break;
        case ((number >= 80) && (number <= 82)):
            letterGrade = "B-";
            break;
        case ((number >= 83) && (number <= 86)):
            letterGrade = "B";
            break;
        case ((number >= 87) && (number <= 89)):
            letterGrade = "B+";
            break;
        case ((number >= 90) && (number <= 92)):
            letterGrade = "A-";
            break;
        case ((number >= 93) && (number <= 96)):
            letterGrade = "A";
            break;
        case ((number >= 97) && (number <= 100)):
            letterGrade = "A+";
            break;
        default:
            alert("Could not compute");
    }
    return letterGrade   
}
function getmygrade_1(number) {
    var letterGrade = "";
    if (number <= 59)
        letterGrade = "F";
    else if (number >= 60 && number <= 69)
        letterGrade = "D";
    else if (number >= 70 && number <= 72)
        letterGrade = "C-";
    else if (number >= 73 && number <= 76)
        letterGrade = "C";
    else if (number >= 77 && number <= 79)
        letterGrade = "C+";
    else if (number >= 80 && number <= 82)
        letterGrade = "B-";
    else if (number >= 83 && number <= 86)
        letterGrade = "B";
    else if (number >= 87 && number <= 89)
        letterGrade = "B+";
    else if (number >= 90 && number <= 92)
        letterGrade = "A-";
    else if (number >= 93 && number <= 96)
        letterGrade = "A";
    else if (number >= 97 && number <= 100)
        letterGrade = "A+";
    return letterGrade   
}

function getmygrade_2(number) {
    var letterGrade = "";
    if (number <= 100)
        letterGrade = "A+";
        if(number <= 96)
            letterGrade = "A";
            if(number <= 92)
                letterGrade = "A-";
                if(number <= 89)
                    letterGrade = "B+";
                    if(number <= 86)
                        letterGrade = "B";
                        if(number <= 82)
                            letterGrade = "B-";
                            if(number <= 79)
                                letterGrade = "C+";
                                if(number <= 76)
                                    letterGrade = "C";
                                    if(number <= 72)
                                        letterGrade = "C-";
                                        if(number <= 69)
                                            letterGrade = "D";
                                            if(number <= 59)
                                                letterGrade = "F";
    return letterGrade  
}
</script>
</body>
</html>
link

answered

tommyBoy's gravatar image

tommyBoy

Tweaked your code a little!

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
<html>
<head>
<title>Untitled Document</title>

<script type="text/javascript">
function process() {
    var NumberGrade = document.getElementById('enterGrade').value;
    var MyGrade = getmygrade(NumberGrade);
    document.getElementById("letterGrade").innerHTML = MyGrade;
}
function getmygrade(number) {
    var letterGrade ="F";
    if (number <= 59)
        letterGrade = "F";
    else if (number >= 60 && number <= 69)
        letterGrade = "D";
    else if (number >= 70 && number <= 72)
        letterGrade = "C-";
    else if (number >= 73 && number <= 76)
        letterGrade = "C";
    else if (number >= 77 && number <= 79)
        letterGrade = "C+";
    else if (number >= 80 && number <= 82)
        letterGrade = "B-";
    else if (number >= 83 && number <= 86)
        letterGrade = "B";
    else if (number >= 87 && number <= 89)
        letterGrade = "B+";
    else if (number >= 90 && number <= 92)
        letterGrade = "A-";
    else if (number >= 93 && number <= 96)
        letterGrade = "A";
    else if (number >= 97 && number <= 100)
        letterGrade = "A+";

    return letterGrade
}
function cleargrade() {
 document.getElementById("letterGrade").innerHTML=""
}
</script></head>
<body>
ENTER SCORE<input type="text" id="enterGrade" onchange="process();" onclick="cleargrade();">&nbsp;</input>
<input type =button value='Calculate Grade' onclick="process();"()></input>
<br>
<br>
GRADE<div id="letterGrade" ></div>
<br>
</body>
</html>
link

answered 2011-11-26 at 19:25:44

experts1's gravatar image

experts1

thanks and that was my bad
This works great!!
also, i need to add another value to my display called quality points so if the number grade is A it will display 1.0 and etc

I though to add this to the cases:
function process() {
    var NumberGrade = document.getElementById("enterGrade");
    var MyGrade = getmygrade(NumberGrade.value);
    var MyGP = getmygrade();
    var Display = document.getElementById("letterGrade");
    var Display2=document.getElementById("gpaGrade");
    Display.innerHTML = MyGrade;
    Display2.innerHTML = MyGP;
}

function getmygrade(number) {
    var letterGrade = "";
    var qp = "";
    switch (true) {
        case (number <= 59):
            letterGrade = "F";
            qp="test";
            break;
link

answered 2011-11-26 at 20:01:37

itconsultant1's gravatar image

itconsultant1

in other words im outputting the grade based on the number input to a html input once my letter grade is written i need to output also addition number in separate DIV tag.
so for grades it should write:

a=1.0
b=2.0
c=3.0

etc

This would be ease is i had to output this into same text box in html but it has to but in a separate div tag
link

answered 2011-11-26 at 20:35:55

itconsultant1's gravatar image

itconsultant1

This is about as tight as you can code the whole thing:

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
<html><head><title>Test</title>
<script type="text/javascript">
var Lgrade = new Array("F","D","C-","C","C+","B-","B","B+","A-","A","A+");
var Ngrade = new Array (60,70, 73, 77, 80, 82, 87, 90, 92, 97, 1001);
function getLgrade(ng)
{
	for (i=0;i<Ngrade.length;i++)
	{
		if (ng<Ngrade[i])
		{
		  	return Lgrade[i];
		}
	}
}
function setLGrade()
{
   nGrade=parseInt(document.forms.f1.e1.value);
   document.forms.f1.e2.value=getLgrade(nGrade);
   document.forms.f1.e3.value=(nGrade<70)? "0.0":(nGrade<80)? "1.0":(nGrade<90)? "2.0": "3.0";
 }
 </script>
 </head>
 <body>
 <form name="f1">
 Enter number grade: <input type="text" name="e1" onkeyup="setLGrade()" value=""></input>
 <span style="margin-left:20px">Letter Grade: <input type="text" name="e2" value=""></input></form>
 <span style="margin-left:20px">Quality Points: <input type="text" name="e3" value=""></input></form>
 </body></html>


I used form inputs for easier testing but you can change it to go to the innerHTML of divs if you want.



Cd&
link

answered 2011-11-26 at 21:32:29

COBOLdinosaur's gravatar image

COBOLdinosaur

Modified for GPA!
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
<html>
<head>
<title>Untitled Document</title>

<script type="text/javascript">
var MyGP ="";
function process() {
    var NumberGrade = document.getElementById('enterGrade').value;
    var MyGrade = getmygrade(NumberGrade);
    document.getElementById("letterGrade").innerHTML = "<big>"+MyGrade+"</big>";
    document.getElementById("gpaGrade").innerHTML = "<big>"+MyGP+"</big>";
}
function getmygrade(number) {
    var letterGrade ="F";
    if (number <= 59) {
        MyGP = "0.00";
        letterGrade = "F";}
    else if (number >= 60 && number <= 69) {
        MyGP = "1.00";
        letterGrade = "D";}
    else if (number >= 70 && number <= 72) {
        MyGP = "1.67";
        letterGrade = "C-";}
    else if (number >= 73 && number <= 76) {
        MyGP = "2.00";
        letterGrade = "C";}
    else if (number >= 77 && number <= 79) {
        MyGP = "2.33";
        letterGrade = "C+";}
    else if (number >= 80 && number <= 82) {
        MyGP = "2.67";
        letterGrade = "B-";}
    else if (number >= 83 && number <= 86) {
        MyGP = "3.00";
        letterGrade = "B";}
    else if (number >= 87 && number <= 89) {
        MyGP = "3.33";
        letterGrade = "B+";}
    else if (number >= 90 && number <= 92) {
        MyGP = "3.67";
        letterGrade = "A-";}
    else if (number >= 93 && number <= 96) {
        MyGP = "4.00";
        letterGrade = "A";}
    else if (number >= 97 && number <= 100) {
        MyGP = "4.33";
        letterGrade = "A+";}

    return letterGrade
}
function cleargrade() {
 document.getElementById("letterGrade").innerHTML="";
 document.getElementById("gpaGrade").innerHTML=""
}
</script></head>
<body>
ENTER SCORE<input type="text" id="enterGrade" onchange="process();" onclick="cleargrade();">&nbsp;</input>
<input type =button value='Calculate Grade' onclick="process();"()></input>
<br>
<br>
GRADE:<div id="letterGrade" ></div>
<br>
GPA:<div id="gpaGrade" ></div>
</body>
</html>
link

answered 2011-11-26 at 21:56:55

experts1's gravatar image

experts1

Your answer
[hide preview]

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Tags:

×1
×104
×141

Asked: 11/26/2011 10:05

Seen: 352 times

Last updated: 12/16/2011 12:23