...

View Full Version : Javascript Form Issue



sara jane
06-15-2010, 12:22 PM
Hi guys,

I'm having an issue with a my form total, I'm getting the same result no matter what positive number I enter. Every other aspect of the code seems to be working ok apart from this.
eg.
If I enter the number 4, the factorial number should read 24 but it's reading 1, its reading 1 no matter what number goes in there.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>



<script type= "text/javascript">

function calcFactorial(factorialNumber) {
var factorialResult = 1;
for (; factorialNumber > 0; factorialNumber --){
factorialNumber = factorialResult * factorialNumber;
}
return factorialResult;
}
</script>
</head>
<frameset cols="100%,*">
<frame name = "fraCalcFactorial" src="calcFactorial.htm" />
<frame src="UntitledFrame-4"></frameset><noframes></noframes>


</html>


I believe the issue is in here somewhere but I'm not sure where. Ironically I'm learning from a book and this is from the Error handling/Debugging chapter.
I'm going well on this book so far and really don't want to have to move forward without fully understanding whats going on.




Any help/comments would be greatly received.

Sara

sara jane
06-15-2010, 12:23 PM
Just in case the issue is on it's coressponding page

<!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">
<title>Untitled Document</title>



<script type= "text/javascript">

function butCalculate_onclick(){
try
{
if(window.top.calcFactorial == null)
throw "This page is not loaded within the correct frameset";
if(document.form1.txtNum1.value == "")
throw "!please enter a value before you calculate its factorial";
if(isNaN(document.form1.txtNum1.value))
throw "!please enter a valid number";
if(document.form1.txtNum1.value < 0)
throw "!Please enter a positive number";

document.form1.txtResult.value =
window.parent.calcFactorial(document.form1.txtNum1.value);
}
catch(exception)
{
if(typeof(exception) == "string")
{
if(exception.charAt(0) =="!")
{
alert(exception.substr(1));
document.form1.txtNum1.focus();
document.form1.txtNum1.select();
}
else
{
alert(exception);
}
}
else
{
alert("the following error occured " + exception.message);
}
}
}
</script>
</head>
<body>
<form action ="" name ="form1">
<input type="text" name="txtNum1" size="3" ? /> factorial is
<input type="text" name="txtResult" size="25" /><br />
<input type="button" value="Calculate Factorial"
name="butCalculate" onclick="butCalculate_onclick()" />

</form>
</body>


</html>


Thanks for looking.

SB65
06-15-2010, 12:40 PM
This:


factorialNumber = factorialResult * factorialNumber;

should be


factorialResult = factorialResult * factorialNumber;

sara jane
06-15-2010, 12:46 PM
Thank you SB65,

I can now get back to the business of learning javascript.

Much appreciated.:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum