Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Oct 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JavaScript Form Help

    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

    Code:
     <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.

  • #2
    Regular Coder
    Join Date
    Dec 2003
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts
    the quickest and easiest way to go about this is like so

    Code:
    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.
    UNDER CONSTRUCTION

  • #3
    New to the CF scene
    Join Date
    Oct 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 :

    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>
    Last edited by lrY.Coder; 10-20-2006 at 02:29 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •