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 13 of 13
  1. #1
    New Coder
    Join Date
    Aug 2002
    Location
    Atlanta
    Posts
    26
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Urgently seeking help on For Loop for Exam!!!

    I need help calculating the average for a Practical Exam
    1) the teacher asked us to create a for loop and prompt the user to enter 10 numerical grade.
    (which I did)
    2) She wants us to find the average of the user's 10 entry, and give them their final grades
    (ex: user enters 80, 90, 70, 90 etc.., the counter will stop prompting for entry after the 10th entry)
    I need to display, your final grade for the class 85, you've got a B)

    I need help figuring out the average formula!

    Please, i need to e-mail the teacher my practical by 10 PM (eastern time).

    Thank you,
    victory1
    Last edited by victory1; 08-18-2002 at 12:43 AM.

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <script>
    // Initialize variables
    var arrGrades = new Array()
    var quantity = 10;
    var total = 0;
    var lGrade = '';
    
    // Prompt user for grades
    for (var i=0; i<quantity; i++)
    	arrGrades[i] = prompt("Enter grade number "+(i+1),"");
    
    // Write entered grade data to page and calculate total
    for (var i in arrGrades) {
    	document.write("Grade "+(parseInt(i)+1)+" = "+arrGrades[i]+"<br>");
    	total += parseInt(arrGrades[i]);
    	}
    
    // Calculate average, rounded
    var average = Math.round(total/arrGrades.length);
    
    // Determine letter grade
    if (average >= 90)
    	lGrade = 'A';
    else if (average >= 80)
    	lGrade = 'B';
    else if (average >= 70)
    	lGrade = 'C';
    else if (average >= 60)
    	lGrade = 'D';
    else
    	lGrade = 'F';
    
    // Write result to page
    document.write("<br>Your average is "+average+". You have a "+lGrade);
    </script>
    Last edited by beetle; 08-18-2002 at 01:30 AM.

  • #3
    New Coder
    Join Date
    Aug 2002
    Location
    Atlanta
    Posts
    26
    Thanks
    2
    Thanked 0 Times in 0 Posts
    It worked!!!
    Thank you, Thank you!!!!

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy

    This forum is an excellent place to ask for help with school assignments, but I would discourage other members from just giving the answer. What's the point of that?

  • #5
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Apologies if anyone disagrees with what I've done. I prefer to 'teach' in the same way that I learn best. I provided this code sample not so victory1 could 'hand it in' but learn from the structure I used and apply it to his/her own code.

  • #6
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    See MUST READ- How to post a JavaScript question!

    I decided a clarification on homework issues was needed, so hopefully this situation won't occur again.

  • #7
    New Coder
    Join Date
    Aug 2002
    Location
    Atlanta
    Posts
    26
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks again beetle, you did nothing wrong and I really appreciate the help!

    Brothercake, Beetle explanation would not have made any sense without the complete format, and for your information, I did not copy his work! I simply look at his format, and realized how he arrived at the answer. I already had the work, i just could not get my average formula to work. My program was simply taking the last entry and dividing it by 10 and in turn, giving every one an 'F'.

    Thanks again, Beetle, this program was 1 of 4 we had to turn in, and asking the board for help was a last minute desperation, since i've been playing with the formula for at least 4 hours.

  • #8
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No I'm sure you didn't. It's cool; I just felt the need to mention it.

  • #9
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Since we're all in the learning mode here...

    Hay, V1 here's a suggestion for that most excellent code provided by beetle...

    Make the code more bullet proof by forcing a valid entry. As it stands the user could enter "z" for example.

    In the "prompt user for grades" part here's what you do.

    1. prompt user for grade
    2. begin a loop that checks for a valid entry
    2a. if the entry isn't valid put out a message.
    2c. prompt user for grade
    2b. loop

    If the above must be repeated xx times wrap all the above in that existing FOR loop.

    Maybe too late for this assignment, but this is a good, basic structure to use for checking user input.

    Hint: Have y'all learned about WHILE loops yet?

  • #10
    Regular Coder
    Join Date
    Jun 2002
    Location
    Depends on the time of year
    Posts
    478
    Thanks
    0
    Thanked 0 Times in 0 Posts
    heh, come to think of it, i've learned more from this forum than from my professors at my University. Now that's scary - I want my money back!!!

    Duniyadnd

  • #11
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    Schools teach Javascript?
    Omnis mico antequam dominus Spookster!

  • #12
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    my program does work..need help

    this is my code which i have done, but the if statement doesnt seem to work. I am not sure why. pls help me on this.

    <html>
    <head>
    <script language="javascript">
    <!--
    function validate()
    var valid=true;

    {
    if(document.code.new.value==" ")

    { alert("please fill up your name");

    document.code.new.focus();
    valid=false;
    }
    }

    //--
    </script>
    </head>
    <body>
    <form name="code" method="post" action="mailto:lcastro@cookwood.com">
    Name :<input type="text" name="new">
    Address:<input type="text" name="address" size=30><br>
    City:<input type="text" name="city">
    State:<input type="text" name="state" size=2 maxlength=2>
    Zipcode:<input type="text" name="zip" size=5 maxlength=5>
    Customer Code:<input type="password" name="code" size=8>
    <hr>Please share any suggestions or comments with us:
    <textarea name="comments" rows=3 cols=65 wrap>comments?</textarea><br>
    <b>Size:</b>
    King<input type="radio" name="size" value="k">
    Queen<input type="radio" name="size" value="q">
    Twin<input type="radio" name="size" value="t">
    Single<input type="radio" name="size" value="s"><br>
    <input type="button" name="submit" value="Order Bed" onclick="validate()">
    <input type="button" name="submit1" value="Start Over">

    </form>
    </body>
    </html>

  • #13
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Why have you hi-jacked and resurrected this ancient thread which has nothing to do with form validation? You should start a new thread of your own.

    new is a reserved word in Javascript.

    function validate()
    var valid=true;

    {

    Form validation of the pattern if (document.code.elements[1].value == "") is barely worthy of the name, and virtually useless, as even a single space, an X or a ? will return false, that is pass the validation. Numeric values, such as zip codes and phone numbers, should be validated as such. Ditto email addresses. This topic has been covered many times before in this forum.

    The trouble with using mailto: to send form results is its unpredictability. The method it is highly dependent on the browser in use and the email client in use (some people have only Yahoo or Hotmail). In particular, your visitor must have Outlook or Outlook Express as the default client for this to work correctly. Even if your visitor is using Internet Explorer, but the default mail client is different (e.g. Eudora), your mailto form will not work. With all of the browser troubles, you're likely to lose about half of your users' messages. Most of the email clients that can successfully send a mail will prompt the user by a security dialog prior to sending - this can scare many users from continuing. Also, what about people with Javascript disabled?

    In addition, if you place an unobfuscated email address in your webpage, the bots will quickly find it and inundate you in spam.


    Please have a look at the forum rules and posting guidelines.


    "Knowledge is of no value unless you put it into practice.”
    Anton Chekhov (Russian playwright and master of the modern short story, 1860-1904)
    Last edited by Philip M; 03-24-2011 at 06:23 PM.


  •  

    Posting Permissions

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