/    Sign up×
Community /Pin to ProfileBookmark

My GPA calculate script problem…… Too tired :(

hello. first i read the stickies and searched for my problem ?
and btw sorry for my poor English ?

i am beginner at javascript. i want to make a script that calculates the GPA… i want to use <select><option> (drop-down). so i want user not to use keyboard..

lets say there are 5 class (in fact there will be more than 50) and because of this i dont want to write 50 times these tags
<option value=A>4</option>
<option value=B>3</option>
.
.
<option value=F>0</option>

i will use loop for this. and here comes my problem..

i will only write 1 <select><option> and javascript make it more. the script multiply grade and credits. then sum them up and at last divide the sum by totalcredit.

the result will be written in <input type=text>
————————————————


this is my code…. of course it isnt working!! and i think this code is totally wrong ?
——————————————————-

<html>
<head>

<script language=”javascript” type=”text/javascript”>
for (i=1 ;i<5 ;i++ )
{document.write(“<form name=creditform”
+[i]
+”>”
+”<select id=creditvalue”+[i]+”>”
+”<option value=1>1</option>”
+”<option value=2>2</option>”
+”</select>”
+”<select id=gradevalue”+[i]+”>”
+”<option value=4>A</option>”
+”<option value=3>B</option>”
+”</select>”
+”</form>”
);}

document.write(“<form name=lastform>”
+”<input type=button value=RESULT onclick=hesaplama();>”
+”<input type=text name=showresult>”
+”</form>”
)

var creditt;[i]=document.getElementById(“creditvalue”+[i])
var creditselect;[i]=creditt[i].options[creditt[i].selectedIndex].value;
var gradee;[i]=document.getElementById(“gradevalue”);
var gradeselect;[i]=gradee[i].options[gradee[i].selectedIndex].value;

multiplyresult= new Array();
multiplyresult= new Array();

multiplyresult[i]=gradeselect[i]*creditselect[i];
totalmultiplyresult=0;
totalmultiplyresult+=multiplyresult[i];
totalcredit=0;
totalcredit+=creditselect[i];
mainresult=totalmultiplyresult/totalcredit;
</script>

<script language=”javascript” type=”text/javascript”>
function hesaplama()
{
document.lastform.showresult.value=mainresult;
}
</script>

</head>
</html>

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@ZeroKilledApr 07.2009 — hell yeah, the script is totally wrong!? first, i don't see the reason why you would use multiple form when only is needed one. having a lot of form make it harder to refer each of them. after all, i guess the result isn't going to be submitted. and second, DOM is far better than document.writing elements. some reference to get you started with DOM: [url=http://www.w3schools.com/htmldom/default.asp]DOM Tutorial[/url] and [url=http://www.w3schools.com/htmldom/dom_reference.asp]DOM Reference[/url]

a sample:
<i>
</i>&lt;html&gt;&lt;head&gt;
&lt;script type='text/javascript'&gt;
onload = function(){
var parent = document.getElementById('course');
var clone = parent.firstChild.cloneNode(true);
for(var i = 0; i &lt; 5; i++){
parent.appendChild(clone.cloneNode(true));
}
}

function gpa(){
var credits = document.getElementsByName('credit');
var grades = document.getElementsByName('grade');
var average = 0, totalcredits = 0;
for(var i = 0; i &lt; grades.length; i++){
average += credits[i].value * grades[i].value;
totalcredits += Number(credits[i].value);
}
document.creditform.result.value = average / totalcredits;
}
&lt;/script&gt;&lt;/head&gt;

&lt;body&gt;
&lt;h1&gt;GPA&lt;/h1&gt;
&lt;form name='creditform'&gt;
&lt;div id='course'&gt;&lt;p&gt;
&lt;select name='credit' onchange='gpa();'&gt;
&lt;option value='3'&gt;3&lt;/option&gt;
&lt;option value='2'&gt;2&lt;/option&gt;
&lt;option value='1'&gt;1&lt;/option&gt;&lt;/select&gt;
&lt;select name='grade' onchange='gpa();'&gt;
&lt;option value='4'&gt;A&lt;/option&gt;
&lt;option value='3'&gt;B&lt;/option&gt;
&lt;option value='2'&gt;C&lt;/option&gt;
&lt;option value='1'&gt;D&lt;/option&gt;
&lt;option value='0'&gt;F&lt;/option&gt;&lt;/select&gt;&lt;/p&gt;&lt;/div&gt;
&lt;p&gt;&lt;input type='text' name='result' /&gt;&lt;/p&gt;
&lt;/form&gt;
&lt;/body&gt;&lt;/html&gt;
Copy linkTweet thisAlerts:
@jwinauthorApr 07.2009 — thank you so much. im discovering your script im trying to understand. thanks again.
×

Success!

Help @jwin spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...