p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/)
-   BOOK: Beginning JavaScript 4th Edition (http://p2p.wrox.com/book-beginning-javascript-4th-edition-529/)
-   -   Problem with Chapter 4, Page 96--try...catch Throwing Errors (http://p2p.wrox.com/book-beginning-javascript-4th-edition/92662-problem-chapter-4-page-96-try-catch-throwing-errors.html)

rhieger April 16th, 2014 01:10 PM

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

stanke October 11th, 2014 04:51 PM

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.

mrected June 11th, 2015 12:03 PM

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>



All times are GMT -4. The time now is 03:42 AM.

Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
2013 John Wiley & Sons, Inc.