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