Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-18-2002, 12:36 AM   PM User | #1
victory1
New Coder

 
Join Date: Aug 2002
Location: Atlanta
Posts: 26
Thanks: 2
Thanked 0 Times in 0 Posts
victory1 is an unknown quantity at this point
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..
victory1 is offline   Reply With Quote
Old 08-18-2002, 01:20 AM   PM User | #2
beetle
Senior Coder

 
Join Date: Aug 2002
Posts: 3,467
Thanks: 0
Thanked 0 Times in 0 Posts
beetle has a little shameless behaviour in the past
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..
beetle is offline   Reply With Quote
Old 08-18-2002, 02:45 AM   PM User | #3
victory1
New Coder

 
Join Date: Aug 2002
Location: Atlanta
Posts: 26
Thanks: 2
Thanked 0 Times in 0 Posts
victory1 is an unknown quantity at this point
It worked!!!
Thank you, Thank you!!!!
victory1 is offline   Reply With Quote
Old 08-18-2002, 04:47 PM   PM User | #4
brothercake
Senior Coder


 
Join Date: Jun 2002
Location: near Oswestry
Posts: 4,508
Thanks: 0
Thanked 0 Times in 0 Posts
brothercake is an unknown quantity at this point
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?
brothercake is offline   Reply With Quote
Old 08-18-2002, 05:04 PM   PM User | #5
beetle
Senior Coder

 
Join Date: Aug 2002
Posts: 3,467
Thanks: 0
Thanked 0 Times in 0 Posts
beetle has a little shameless behaviour in the past
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.
beetle is offline   Reply With Quote
Old 08-18-2002, 05:31 PM   PM User | #6
jkd
Senior Coder

 
jkd's Avatar
 
Join Date: May 2002
Location: metro DC
Posts: 3,163
Thanks: 1
Thanked 18 Times in 18 Posts
jkd will become famous soon enough
See http://www.codingforums.com/showthre...&threadid=2090

I decided a clarification on homework issues was needed, so hopefully this situation won't occur again.
__________________
jasonkarldavis.com
jkd is offline   Reply With Quote
Old 08-18-2002, 07:04 PM   PM User | #7
victory1
New Coder

 
Join Date: Aug 2002
Location: Atlanta
Posts: 26
Thanks: 2
Thanked 0 Times in 0 Posts
victory1 is an unknown quantity at this point
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.
victory1 is offline   Reply With Quote
Old 08-21-2002, 12:41 PM   PM User | #8
brothercake
Senior Coder


 
Join Date: Jun 2002
Location: near Oswestry
Posts: 4,508
Thanks: 0
Thanked 0 Times in 0 Posts
brothercake is an unknown quantity at this point
No I'm sure you didn't. It's cool; I just felt the need to mention it.
brothercake is offline   Reply With Quote
Old 08-21-2002, 01:48 PM   PM User | #9
RadarBob
Regular Coder

 
Join Date: Jun 2002
Location: Round Rock, Texas
Posts: 443
Thanks: 0
Thanked 0 Times in 0 Posts
RadarBob is an unknown quantity at this point
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?
RadarBob is offline   Reply With Quote
Old 08-31-2002, 02:53 AM   PM User | #10
duniyadnd
Regular Coder

 
Join Date: Jun 2002
Location: Depends on the time of year
Posts: 478
Thanks: 0
Thanked 0 Times in 0 Posts
duniyadnd is an unknown quantity at this point
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
duniyadnd is offline   Reply With Quote
Old 10-06-2002, 05:20 AM   PM User | #11
Mhtml
Senior Coder

 
Mhtml's Avatar
 
Join Date: Jun 2002
Location: Sydney, Australia
Posts: 3,531
Thanks: 0
Thanked 1 Time in 1 Post
Mhtml is an unknown quantity at this point
Schools teach Javascript?
__________________
Omnis mico antequam dominus Spookster!
Mhtml is offline   Reply With Quote
Old 03-24-2011, 05:29 PM   PM User | #12
breentha19
New to the CF scene

 
Join Date: Mar 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
breentha19 is an unknown quantity at this point
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>
breentha19 is offline   Reply With Quote
Old 03-24-2011, 05:58 PM   PM User | #13
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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..
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:22 AM.


Advertisement
Log in to turn off these ads.