Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: question on For Loop Formula?

1. ## 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

2. Code:
```<script>
// Initialize variables
var quantity = 10;
var total = 0;

for (var i=0; i<quantity; i++)

// Write entered grade data to page and calculate total
for (var i in arrGrades) {
}

// Calculate average, rounded

if (average >= 90)
else if (average >= 80)
else if (average >= 70)
else if (average >= 60)
else

// Write result to page
</script>```

3. It worked!!!
Thank you, Thank you!!!!

4. 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. 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. 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. 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. No I'm sure you didn't. It's cool; I just felt the need to mention it.

9. 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.

2. begin a loop that checks for a valid entry
2a. if the entry isn't valid put out a message.
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. 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!!!

11. Schools teach Javascript?

12. ## 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>
<script language="javascript">
<!--
function validate()
var valid=true;

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

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

//--
</script>
<body>
<form name="code" method="post" action="mailto:lcastro@cookwood.com">
Name :<input type="text" name="new">
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>
<b>Size:</b>
<input type="button" name="submit" value="Order Bed" onclick="validate()">
<input type="button" name="submit1" value="Start Over">

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

13. 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)

#### Posting Permissions

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