What's wrong with my factorial calculator code !

Discussion in 'JavaScript' started by KrøniiK, Jan 21, 2011.

  1. #1
    Please help here are my two files:

    calcfactorialtopframe.htm
    <html>
    <head>
    <title>Example</title>
    <script type ="text/javascript">
    function calcFactorial(factorialNumber)
    {
    var factorialResult = 1;
    for (; factorialNumber > 0; factorialNumber--)
    {
    factorialResult = factorialResult * factorialNumber;
    }
    return factorialResult;
    }
    </script>
    </head>
    <frameset cols="100%,*">
    <frame name=fraCalcFactorial" src="calcfactorial.htm" />
    </frameset>
    </html>
    		
    Code (markup):
    calcfactorial.htm
    <html>
    <head>
    <title>Example</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>
    Code (markup):

    When I launch the first one and in the first box when I put 4 it should give me a result in the second box of 24.. but when I put a number in the first box the second box doesn't give me the results so I kept on checking the script but couldn't find the error, any help would be appreciated.
     
    KrøniiK, Jan 21, 2011 IP
  2. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #2
    Change the file calcfactorial.htm to:

    
    <html>
    <head>
    <title>Example</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>
    
    Code (JavaScript):
     
    s_ruben, Jan 22, 2011 IP
    KrøniiK likes this.
  3. KrøniiK

    KrøniiK Peon

    Messages:
    262
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    yeh it works now but since I am doing this as a new starter can you tell me what was wrong with mine ?
     
    KrøniiK, Jan 22, 2011 IP
  4. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #4
    The red lines are the wrong lines. Compare it with my version and you'll see.

    
    <html>
    <head>
    <title>Example</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)
    		[COLOR="red"]}[/COLOR]
    			if (typeof(exception) == "string")
    			[COLOR="red"]}[/COLOR]
    				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>
    [COLOR="red"]</body>[/COLOR]
    <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>
    
    Code (markup):
     
    s_ruben, Jan 22, 2011 IP
  5. KrøniiK

    KrøniiK Peon

    Messages:
    262
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Just noticed it, thank you very much for helping me out.
     
    KrøniiK, Jan 22, 2011 IP