...

View Full Version : JavaScript Form Help



lrY.Coder
10-19-2006, 03:52 PM
Ok I am new here but I have been looking around. I am trying to make a form for my friends to fill in which will calculate their average score. Currently my script body is something like this


<HTML>
<HEAD>
<TITLE>Lab 1</TITLE>
</HEAD>

<BODY bgcolor="C0C0C0">

<SCRIPT language="JavaScript">
<!--
alert("Please enter your scores. Thank You.")

var english = ??
var maths = ??
var geography = ??
var chinese = ??
var lit = ??
var psci = ??
var lsci = ??
var art = ??
var hist = ??
var mep = ??
var total = english + maths + geography + chinese + lit + psci + lsci + art + hist + mep
var subjects = 10

document.write("Your average is " + total / + subjects)
//-->
</SCRIPT>

</BODY>
</HTML>

So what I was thinking was is it possible to make a form that will fill in the variables after they have typed it in and click ok, the variables will the be added and calculated to give the average. Any sort of help will be appreciated.

warhammerdude20
10-19-2006, 04:14 PM
the quickest and easiest way to go about this is like so



var english=parseInt(prompt("Enter your English mark","enter here"));


and do that for all the subjects.

ParseInt() a written number or a "string" into an actual number that can be used in equatons.

lrY.Coder
10-20-2006, 12:50 AM
Ok I have tried what you gave me and it is ok. The only problem is that the mark which is entered should replace the variable ?? for english. So that is the part which I am having trouble with. This is the new code :


<HTML>
<HEAD>
<TITLE>Your Average for the year</TITLE>
</HEAD>

<BODY bgcolor="C0C0C0">

<SCRIPT language="JavaScript">
<!--
alert("Please enter your scores. Thank You.")
var english=parseInt(prompt("Enter your English mark",""));
var maths=parseInt(prompt("Enter your Maths mark",""));
var geography=parseInt(prompt("Enter your Geography mark",""));
var chinese=parseInt(prompt("Enter your Chinese mark",""));
var lit=parseInt(prompt("Enter your Literature mark",""));
var psci=parseInt(prompt("Enter your Physical Science mark",""));
var lsci=parseInt(prompt("Enter your Life Science mark",""));
var art=parseInt(prompt("Enter your Art mark",""));
var hist=parseInt(prompt("Enter your History mark",""));
var mep=parseInt(prompt("Enter your MEP mark (Please leave blank if you do not take MEP)",""));

var english = 0
var maths = 0
var geography = 0
var chinese = 0
var lit = 0
var psci = 0
var lsci = 0
var art = 0
var hist = 0
var mep = 0
var total = english + maths + geography + chinese + lit + psci + lsci + art + hist + mep
var ntotal = english + maths + geography + chinese + lit + psci + lsci + art + hist
var subjects = 10
var nsubjects = 9

document.write("For MEP students, your average is : <bR>")
document.write(+ total / + subjects)
document.write("<bR>For non-MEP students, your average is : <bR>")
document.write(+ ntotal / + nsubjects)
//-->
</SCRIPT>

</BODY>
</HTML>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum