[Javascript] JavaScriptNewbie- can't get info to write to TextBox
Steve
extstarrfam at cox-internet.com
Sun Mar 10 16:15:49 CST 2002
I am calling a recursive function getFactorial(n) from the function Display(n) and trying to get it to display the answer in the TextBox fresult. If you look at the code you can see what I have tried. At the moment, when 6 is entered the alert message says "anwser equals __________". The code at the bottom works since it processes onLoad, but the first code is my problem code.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.O1 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD><TITLE> Recursive Function pg 192- Factorial of a Positive Integer</TITLE>
<!--The original simply display the answer on the form upon loading.-->
<SCRIPT LANGUAGE="javascript1.3" TYPE="text/javascript">
<!--
// If 6 is entered, the answer is 720. It should appear in this.form.fresult.value.
// What appears in fresult is 'undefined'.
var n;
function getFactorial(n) {
if(n==6) {
alert("n equals " + n);
}
var Result = "";
if(n > 0) {
result = n * getFactorial(n-1);
}else if(n == 0) {
Result = 1;
}else{
Result = null;
}
return (Result);
}
//answer = getFactorial(n); this gets a 'n is null' message
//function Display(n) {
//var n;
//var answer;
//document.doIt.fresult.value = getFactorial(n);
//getFactorial(n);
//answer = getFactorial(n);
//alert("answer equals " + answer);
//document.doIt.fresult.value = getFactorial(n);
//document.doIt.fresult.value = answer;
//}
//-->
</SCRIPT>
</HEAD>
<body>
<script text="text/javascript">
<!--
function Display(n) {
var n;
var answer;
//document.doIt.fresult.value = getFactorial(n);
//getFactorial(n);
answer = getFactorial(n);
alert("answer equals " + answer);
//document.doIt.fresult.value = getFactorial(n);
//document.doIt.fresult.value = answer;
}
//-->
</script>
<h3>Compute factorial n! of a positive integer number n</h3>
<hr>
<form NAME="doIt">
Enter integer n:
<input type="text" name="fn" size="2">entry:
<input type="text" name="fresult" size="40">
<br>
<input type="button" name="compute" value="Compute n!" onClick="Display(this.form.fn.value)">
</form>
</body>
</HTML>
CODE THAT WORKS ON LOAD:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.O1 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD><TITLE>Factorial of a Positive Integer</TITLE>
<SCRIPT LANGUAGE="javascript1.3" TYPE="text/javascript">
<!--
function getFactorial(n) {
var result = "";
if(n > 0) {
result = n * getFactorial(n-1);
}else if(n == 0) {
result = 1;
}else{
result = null;
}
return(result);
}
// -->
</SCRIPT>
</HEAD>
<BODY>
<SCRIPT TYPE="text/javascript">
<!--
number = 6;
var answer = "";
answer = getFactorial(number);
document.write("<BR>The factorial of " + number + " is: " + answer + '<BR>');
// -->
</SCRIPT>
<hr>
<P>To find the factorial of a positve integer (represented by n), you find the product of all integers 1 through n. To find the factorial of 6, written 6!, calculate the following.<BR>
6! = 6 x 5 x 4 x 3 x 2 x 1<BR>
= 720</P>
<HR>
<H3 ALIGN="center">Here is the code for the display above.</H3>
<P>Please remember that the left and right arrows used in HTML are replaced with paranethesis so that the code can be displayed in an orderly fashion..</P>
<CODE STYLE="color: #0000CC; font-size: x-small; font-weight: bold;">
<PRE>
(SCRIPT LANGUAGE="javascript1.3" TYPE="text/javascript")
(!--
function getFactorial(n) {
var result = "";
if(n > 0) {
result = n * getFactorial(n-1);
document.write("result as it occurs in the function equals: " + result + '(BR)');
}else if(n == 0) {
result = 1;
}else{
result = null;
}
return(result);
}
// --)
(/SCRIPT)
(/HEAD)
(BODY)
(SCRIPT TYPE="text/javascript")
(!--
number = 6;
var answer = "";
answer = getFactorial(number);
document.write("(BR)The factorial of " + number + " is: " + answer + '(BR)');
// --)
(/SCRIPT)
</PRE>
</CODE.
</BODY>
</HTML>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.evolt.org/pipermail/javascript/attachments/20020310/028a1a4c/attachment.htm>
More information about the Javascript
mailing list