Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > BOOK: Beginning JavaScript 4th Edition
|
BOOK: Beginning JavaScript 4th Edition
This is the forum to discuss the Wrox book Beginning JavaScript, 4th Edition by Paul Wilton, Jeremy McPeak; ISBN: 978-0-470-52593-7
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning JavaScript 4th Edition section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 16th, 2014, 12:10 PM
Authorized User
 
Join Date: Jul 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rhieger
Default Problem with Chapter 4, Page 96--try...catch Throwing Errors

Dear Sir/Ms.,

I was referred to this forum by Wrox customer support.

I am a student of Web Design/Development and I am working with your textbook, Beginning JavaScript 4th Edition, by Paul Wilton and Jeremy McPeak.

Obviously, web standards change rapidly and so particularly JavaScript code can possibly become outdated. I am wondering if this is the case with your exercise on page 96 of the text, "Try It Out try...catch and Throwing Errors."

My concerns are as follows:

1. Obviously the frameset document type is outdated. In fact, HTML5 no longer even supports this document type. Is there a way to revise the code of this example so that it works in a single document?

2. As far as I know, the name attribute has been deprecated or obsolete altogether since the XHTML standard, yet it appears within the XHTML form in this example. According to all online sources I can find, the name attribute should be replaced with the id attribute. Is this true?

3. When I attempt to execute the code, no matter what I do, I am presented with the following error message in an alert box:

"The following error occurred Cannot read property 'txtNum1' of undefined"

I have gone over and over this code and cannot find any error in my code. I have also tried the code from your code repository and find that this also produces errors particularly in Chrome.

Here is my code for both files involved:

calcfactorialtopframe.html
===================

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">
 
 	<!-- This document is structured using the XHTML Frameset, which is now
    	 considered very close to obsolete. Though sites exist that still use
         it, the emerging standard of HTML5 does not even support it.
         
         I am a little mystified why the authors would even have included such
         an example in a beginner's text on JavaScript.
         
     -->
     
     <head>
     
     	<title>try&hellip;catch Example</title>
        
        <!-- The following JavaScript function is called by the frame's source page -->
        
        <script type="text/javascript">
		
			function calcFactorial(factorialNumber) {
				
				// Declare variable to hold initial value of the factorial result.
				
				var factorialResult = 1;
				
				for (; factorialNumber > 0; factorialNuumber--) {
					
					factorialResult = factorialResult * factorialNumber;
					
				}	// end for
				
				// Return to calling function the resulting value.
				
				return factorialResult;
				
			}	// end calcFactorial(factorialNumber)
		
		</script>
     
     </head>
     
     <frameset rows="*,*" cols="100%">
     	<frame id="fraCalcFactorial" src="calcfactorial.html" />
	</frameset>
 
 </html>
calcfactorial.html
=============

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">
 
 	<head>
    
    	<title>try&hellip;catch Example</title>
        
        <!-- NOTE: This placement of a script embedded in the head of an HTML document
        	 is no longer in keeping with best practices, and can tend to make for
             slower processing with large scripts (whhich this is not).
         -->
         
         <script type="text/javascript">
		 
		 	/* The following function takes user input in a screen form, asking the
			   user to input a positive number for which it will calculate the
			   factorial. Assuming no error state, the script fills in the value of
			   the factorial in the screen form.
			*/
			
			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);
					
				}	// end try
				
				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);
							
						}	// end inner if-else
						
					}	else {
						
						alert("The following error occurred " + exception.message);
						
					}	// end outer if-else
					
				}	// end catch
				
			}	// end butCalculate_onclick()
		 
		 </script>
    
    </head>
    
    <body>
    
    	<!-- User Entry Form for Desired Factorial -->
        
        <form action="#" id="form1">
        	<input type="text" id="txtNum1" size="3" /> factorial is
            <input type="text" id="txtResult" size="25" /><br />
            <input type="button" value="Calculate Factorial"
             id="butCalculate" onclick="butCalculate_onclick();" />
        </form>
    
    </body>
 
 </html>

Any assistance you can provide is greatly appreciated.

Sincerely,

Robert Hieger
 
Old October 11th, 2014, 03:51 PM
Registered User
 
Join Date: Feb 2014
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Try this

For start change your code
var factorialResult = 1;
for (; factorialNumber > 0; factorialNuumber--)
into this for(var factorialResult = 0; factorialNumber > 0; factorialNuumber++)
if you try to check more input tags then you need factorialNuumber++
in your for loop you create factorialNuumber is 1 and if he > 0 then decrement.
 
Old June 11th, 2015, 11:03 AM
Registered User
 
Join Date: Jun 2015
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default This will work

Doesn't make use of frames so obviously the frame errors won't be thrown, but the rest of it functions within 1 page...

Code:
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Calculate Factorial</title>
<script>
function calcFactorial(factorialNumber){
	var factorialResult=1;
	for (; factorialNumber > 0; factorialNumber--){
		factorialResult=factorialResult*factorialNumber;
	}
	return factorialResult;
}

function butCalculate_onclick(){
	try{
		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.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>





Similar Threads
Thread Thread Starter Forum Replies Last Post
Page 96 Throwing errors Kim_arg_js BOOK: Beginning JavaScript 4th Edition 0 July 6th, 2010 10:27 AM
25-8 C# Example Throwing Errors mhall BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9 0 February 3rd, 2009 03:25 PM
Validator controls throwing JavaScript errors Bob Bedell ASP.NET 2.0 Professional 7 December 31st, 2007 06:05 AM
MySQL Fatal Error Chap 3, Pg 96-96 joshkosmala BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 2 January 8th, 2007 01:55 PM
Connecting to the MySQLServer - page 96 sleepyd BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 5 July 16th, 2005 06:41 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.