View Full Version : Getting sum of four numbers.
psyche_028
04-28-2005, 08:57 PM
<BODY>
<%
function GetAve(intExam1, intExam2, intExam3, intExam4)
{
return (intExam1 + intExam2 + intExam3 + intExam4);
}
%>
<CENTER>
<%
Response.write("<H3>Grade Report for<\/H3> " + Request.Form("txtName<BR>"));
Response.write("Exam #1: "+ Request.Form("txtExam1") + "%<BR>");
Response.write("Exam #2: "+ Request.Form("txtExam2") + "%<BR>");
Response.write("Exam #3: "+ Request.Form("txtExam3") + "%<BR>");
Response.write("Exam #4: "+ Request.Form("txtExam4") + "%<BR>");
%>
<%
var x1=0,x2=0,x3=0,x4=0, Ave=0;
x1 = Request.Form("txtExam1");
x2 = Request.Form("txtExam2");
x3 = Request.Form("txtExam3");
x4 = Request.Form("txtExam4");
Ave = GetAve(x1,x2,x3,x4);
Response.write("Average: " + Ave);
Session.Abandon();
%>
</BODY>
</CENTER>
----- i tried its using eval but it still dont work. i need to solve this asap. M neophyte in JScript programing.
BaldEagle
04-28-2005, 10:14 PM
Frrom what I can see you are never doing anything to get the average. You are summing the grades but I see no division for the average.
BaldEagle
[edit]
I should be slapped upside the head for not paying attention. What are you trying to do here? Javascript is a client side language. You need a server side language like vbscript or php. These <% ... %> symbols denote server side execution will take place.
Start here for some basic ASP lessons and then google "javascript tutorial" for the rest.
http://www.w3schools.com/asp/default.asp
BaldEagle
psyche_028
04-28-2005, 10:22 PM
Youre right. Sorry for that. Wrong variable name. Im just trying to get the sum. For example I input 4 90. The result will be "90909090"
Please help me with this one. :confused:
BaldEagle
04-28-2005, 10:51 PM
Refer to my previous post. Take a Look at this
<%
Dim x1,x2,x3,x4,Avg,Sum
x1 = Csng(Request.Form("txtExam1"))
x2 = Csng(Request.Form("txtExam2"))
x3 = Csng(Request.Form("txtExam3"))
x4 = Csng(Request.Form("txtExam4"))
Response.write("<H3>Grade Report for<\/H3> " & Request.Form("txtName"))
Response.Write("<br><br>")
Response.write("Exam #1: " & x1 & "%<BR>")
Response.write("Exam #2: " & x2 & "%<BR>")
Response.write("Exam #3: " & x3 & "%<BR>")
Response.write("Exam #4: " & x4 & "%<BR>")
Sum = x1 + x2 + x3 + x4
Avg = Sum / 4
Response.Write("Sum = " & Sum)
Response.Write("<br><br>")
Response.Write("Average = " & Avg)
%>
BaldEagle
psyche_028
04-28-2005, 11:17 PM
Thanks for your advice. But Im already studying ASP, and this problem of mine has something to do with this. But Ill check the link and Im trying your codes.
Thank you so much for your help! Cheers!
JScript, VBScript and even PerlScript can all be used in ASP. (Using the @language or setting it globally in IIS.)
Syntactically, there is nothing wrong with what you're doing. However, the meaning of what you've done is not what you intended it to be.
You are passing string variables to the function. Request.form always returns a string. Using the + operator on strings simply concatenates them. You must parse the integer first.
x1 = parseInt(Request.form("txtExam1"));
x2 = parseInt(Request.form("txtExam2"));
//etc...
Ave = GetAve(x1, x2, x3, x4);
As BaldEagle stated, GetAve doesn't actually calculate an average, but once you get this working I'm sure you'll start working on changing GetAve.
Also, you should make sure the variables you're retrieving with Request.form contain values before calling parseInt.
Regards
BaldEagle
04-29-2005, 01:38 PM
Zee, Since I am a learner myself could you elaborate a little on how to run serverside js. I can't make it work on my platform but since it is possible I would like to know how. I've grown tired of vbscript.
Oh, and psyche_028, I apologize for the bum info.
BaldEagle
or should I start a new thread.
BaldEagle,
Set the @language directive to JScript at the top of your ASP script,
<% @language = JScript %>
<html>
....
<%
//uses JScript!
%>
</html>
You can also mix JScript and VBScript on the same page using script tags...
<script language="JScript" runat="server">
//javascript code
function someFunction(someParam) {
//do stuff
}
</script>
<%
'call a JS function from VBS
someFunction("An argument")
%>
Regards
BaldEagle
04-30-2005, 10:00 AM
I have tried that and it doesn't work for me. I do not use IIS as I have XP Home and use an aftermarket ASP server called Xeneo. Could that be the reason it doesn't work?
BaldEagle
It could very well be... never really tried it on anything else! Only way to find out would be to read its docs or contact Xeneo folks. Poor poor fellow with XP Home :P.
Regards
vBulletin v3.0.3, Copyright ©2000-2012, Jelsoft Enterprises Ltd.