inherit
27278
0
Aug 3, 2024 9:13:18 GMT -8
Josh
Apple iManiac / eBay Addict
12,347
July 2004
jwd41190
|
Post by Josh on Mar 10, 2010 16:34:13 GMT -8
I am having problems with my code with getting the 10 numbers that the user prompts to average and display correctly. Does anyone have any idea what I am doing wrong?
Here is what I have so far:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Lab 5.4</title> </head> <body> <form id="form1" runat="server"> <div> <script type="text/javascript">
var linebreak = "<br />"; document.write("Grades inputed below:"); document.write(linebreak);
for(i = 0; i < 10; i++){ grade = prompt("Please enter a grade", ""); document.write("Grade " + (i + 1) + ": " + grade); document.write(linebreak); Total = grade++; }
document.write("Average Grade: " + (Total/10));
</script>
</div> </form> </body> </html>
Thanks so much!
|
|
inherit
77753
0
Jul 18, 2024 12:23:50 GMT -8
Bob
2,623
April 2006
bobbyhensley
|
Post by Bob on Mar 10, 2010 17:27:13 GMT -8
I am having problems with my code with getting the 10 numbers that the user prompts to average and display correctly. Does anyone have any idea what I am doing wrong? Here is what I have so far: <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Lab 5.4</title> </head> <body> <form id="form1" runat="server"> <div> <script type="text/javascript">
var linebreak = "<br />"; document.write("Grades inputed below:"); document.write(linebreak);
for(i = 0; i < 10; i++){ grade = prompt("Please enter a grade", ""); document.write("Grade " + (i + 1) + ": " + grade); document.write(linebreak); Total = grade++; }
document.write("Average Grade: " + (Total/10));
</script>
</div> </form> </body> </html>
Thanks so much! The prompt is returning a string. Pass the grade variable through parseInt () and you'll have what you're looking for. Well, almost. Total = grade++;You don't want to be redefining the total every single loop, you want a running tally. And grade++ is incrementing the grade variable, something you shouldn't be doing here either. <script type="text/javascript">
var linebreak = "<br />";
document.write("Grades inputed below:"); document.write(linebreak);
var total = 0;
for (var i = 0; i < 10; i++) { grade = prompt ("Please enter a grade", ""); document.write ("Grade " + (i + 1) + ": " + grade + linebreak); total += parseInt(grade); }
document.write("Average Grade: " + (total / 10));
</script> The += operator says "Add RHS to the current value of LHS." And parseInt has a couple uses ( read up on it here) but what it's doing here is converting a string to an integer.
|
|
inherit
27278
0
Aug 3, 2024 9:13:18 GMT -8
Josh
Apple iManiac / eBay Addict
12,347
July 2004
jwd41190
|
Post by Josh on Mar 10, 2010 18:25:19 GMT -8
Ok,thanks I got it working now
|
|