Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jun 2010
    Location
    London
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Javascript Form Issue

    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.
    Code:
    <!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

  • #2
    New Coder
    Join Date
    Jun 2010
    Location
    London
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Just in case the issue is on it's coressponding page
    Code:
     <!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.

  • #3
    Senior Coder
    Join Date
    Feb 2009
    Location
    Ilkley, West Yorkshire, UK
    Posts
    2,957
    Thanks
    9
    Thanked 727 Times in 721 Posts
    This:

    Code:
    	factorialNumber = factorialResult * factorialNumber;
    should be

    Code:
    factorialResult = factorialResult * factorialNumber;

  • Users who have thanked SB65 for this post:

    sara jane (06-15-2010)

  • #4
    New Coder
    Join Date
    Jun 2010
    Location
    London
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thank you SB65,

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

    Much appreciated.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •