learning JavaScript - Need help with an exercise

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sean McCourt

    #1

    learning JavaScript - Need help with an exercise

    Hi I am doing a JavaScript course and learning from the recommed book
    (JavaScript 3rd Edition by Don Gosslin)

    Below is one of the exercises from the book. I get this error message
    when I try to use the calculator.

    "document.Calcu late.Input is null or not an object"

    Can someone please tell me why this is?

    I have checked and double checked that my code is exactly the same as
    the book.


    Thanks






    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

    <html>
    <head>
    <title>Calculat or exercise P120</title>
    <META NAME="Generator " CONTENT="Stone' s WebWriter 3.5">
    <meta http-equiv="content-type" content="text/html;
    charset=iso-8859-1">
    <script type="text/javascript">
    <!-- hide from incompatible browsers

    /*
    the function is used to update the inputString variable above
    x += y is the same as x = x + y
    the variable named inputString contains the operands and operators of
    the calculation
    after the calculation is added to the input string variable, the
    calculation is performed using the eval() function
    the function updateString() accepts a single value representing a
    number or operator
    the value is then added to the inputString variable using the +=
    assignment operator
    after the inputString variable is updated, it is assigned the value of
    a text box in the form

    go to validator.w3.or g/file-upload.html to validate this document

    */

    var inputString = "";
    function updateString(va lue)
    {
    inputString += value;
    document.Calcul ator.Input.valu e = inputString;
    }

    // Stop hiding from incompatible browsers -->
    </script>
    </head>

    <body>

    <form name="Calculato r" action =""> <!-- adds a form element -->
    <input type="text name="Input" /><br /><br /> <!-- adds an input text
    box -->

    <!-- // each element along with the other elements sends a value to
    the updateString() function using an onclick method -->

    <input type="button" name="plus" value=" + " onclick="update String(' +
    ')" />
    <input type="button" name="minus" value=" - " onclick="update String('
    - ')" />
    <input type="button" name="times" value=" x " onclick="update String('
    * ')" />
    <input type="button" name="div" value=" / " onclick="update String(' /
    ')" />
    <input type="button" name="mod" value=" mod " onclick="update String('
    % ')" /><br />
    <input type="button" name="zero" value=" 0 " onclick="update String(' 0
    ')" />
    <input type="button" name="one" value=" 1 " onclick="update String(' 1
    ')" />
    <input type="button" name="two" value=" 2 " onclick="update String(' 2
    ')" />
    <input type="button" name="three" value=" 3 " onclick="update String('
    3 ')" />
    <input type="button" name="four" value=" 4 " onclick="update String(' 4
    ')" /><br />
    <input type="button" name="five" value=" 5 " onclick="update String(' 5
    ')" />
    <input type="button" name="six" value=" 6 " onclick="update String(' 6
    ')" />
    <input type="button" name="seven" value=" 7 " onclick="update String('
    7 ')" />
    <input type="button" name="eight" value=" 8 " onclick="update String('
    8 ')" />
    <input type="button" name="nine" value=" 9 " onclick="update String(' 9
    ')" />
    <br />

    <!-- // the onclick event for the calc button performs the calculation
    using the eval() function with the input string variable -->
    <!-- // the calculated value is then assigned as the value of the
    input text box -->

    <input type="button" name="point" value=" . " onclick="update String('
    .. ')" />
    <input type="button" name="clear" value=" clear "
    onclick="docume nt.Calculator.I nput.value=''; inputString=''" />
    <input type="button" name="calc" value=" = "
    onclick="docume nt.Calculator.I nput.value=eval (inputString);
    inputString=''" />

    </form>

    </body>

    </html>
  • Randy Webb

    #2
    Re: learning JavaScript - Need help with an exercise

    Sean McCourt wrote:[color=blue]
    > Hi I am doing a JavaScript course and learning from the recommed book
    > (JavaScript 3rd Edition by Don Gosslin)[/color]

    If the code you have posted is representative of what the book teaches
    you, you need a new book.
    [color=blue]
    > Below is one of the exercises from the book. I get this error message
    > when I try to use the calculator.
    >
    > "document.Calcu late.Input is null or not an object"
    >
    > Can someone please tell me why this is?[/color]

    Yes.
    [color=blue]
    > I have checked and double checked that my code is exactly the same as
    > the book.[/color]

    You missed the one thing killing it, that validating the HTML would have
    caught.
    <--snip-->

    [color=blue]
    > go to validator.w3.or g/file-upload.html to validate this document[/color]

    From your own code, had you done that it would have pointed out the
    problem.

    <--snip-->
    [color=blue]
    > <input type="text name="Input" /><br /><br /> <!-- adds an input text[/color]

    type="text" note the closing " after the word text that you are missing.

    <--snip-->

    That is ignoring the <!-- //--> that are *not* needed in modern
    browsers. It also ignores the eval usage, although it could be argued
    that the use given is an "adequate" use of eval, there are better
    methods to creating a JS driven calculator.

    --
    Randy
    comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly

    Comment

    • Randell D.

      #3
      Re: learning JavaScript - Need help with an exercise

      Sean McCourt wrote:
      [color=blue]
      > Hi I am doing a JavaScript course and learning from the recommed book
      > (JavaScript 3rd Edition by Don Gosslin)
      >
      > Below is one of the exercises from the book. I get this error message
      > when I try to use the calculator.
      >
      > "document.Calcu late.Input is null or not an object"
      >
      > Can someone please tell me why this is?
      >
      > I have checked and double checked that my code is exactly the same as
      > the book.
      >
      >
      > Thanks
      >[/color]

      I note Randy has replied with a solution, however since we're in the
      same boat (but I'm a little ahead of you learning javascript) I find
      the Mozilla web browser handy for helping me solve problems as it has a
      Javascript console that gives you better error messages... thus, I
      suggest you go to http://www.mozilla.org and near the lower right
      corner, you'll see a link for Mozilla 1.7.5... Download it and use it as
      your browser... If you want to see any errors in your codeing, select
      Tools, Web Development, Javascript Console from the menu and it'll save
      you posting here and waiting for replies...

      Best of luck... you'll reach a few bumps along the way but don't be put
      off - javascript is rather useful for your skillset...

      randelld

      Comment

      • Sean

        #4
        Re: learning JavaScript - Need help with an exercise

        Thank you both for your help



        On Mon, 14 Mar 2005 14:15:25 +1100, Sean McCourt <smc@ihug.com.a u>
        wrote:
        [color=blue]
        >Hi I am doing a JavaScript course and learning from the recommed book
        >(JavaScript 3rd Edition by Don Gosslin)
        >
        >Below is one of the exercises from the book. I get this error message
        >when I try to use the calculator.
        >
        >"document.Calc ulate.Input is null or not an object"
        >
        >Can someone please tell me why this is?
        >
        >I have checked and double checked that my code is exactly the same as
        >the book.
        >
        >
        >Thanks
        >
        >
        >
        >
        >
        >
        ><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        >"http://www.w3.org/TR/html4/loose.dtd">
        >
        ><html>
        ><head>
        ><title>Calcula tor exercise P120</title>
        ><META NAME="Generator " CONTENT="Stone' s WebWriter 3.5">
        ><meta http-equiv="content-type" content="text/html;
        >charset=iso-8859-1">
        ><script type="text/javascript">
        ><!-- hide from incompatible browsers
        >
        >/*
        >the function is used to update the inputString variable above
        >x += y is the same as x = x + y
        >the variable named inputString contains the operands and operators of
        >the calculation
        >after the calculation is added to the input string variable, the
        >calculation is performed using the eval() function
        >the function updateString() accepts a single value representing a
        >number or operator
        >the value is then added to the inputString variable using the +=
        >assignment operator
        >after the inputString variable is updated, it is assigned the value of
        >a text box in the form
        >
        >go to validator.w3.or g/file-upload.html to validate this document
        >
        >*/
        >
        >var inputString = "";
        >function updateString(va lue)
        > {
        > inputString += value;
        > document.Calcul ator.Input.valu e = inputString;
        > }
        >
        >// Stop hiding from incompatible browsers -->
        ></script>
        ></head>
        >
        ><body>
        >
        ><form name="Calculato r" action =""> <!-- adds a form element -->
        ><input type="text name="Input" /><br /><br /> <!-- adds an input text
        >box -->
        >
        ><!-- // each element along with the other elements sends a value to
        >the updateString() function using an onclick method -->
        >
        ><input type="button" name="plus" value=" + " onclick="update String(' +
        >')" />
        ><input type="button" name="minus" value=" - " onclick="update String('
        >- ')" />
        ><input type="button" name="times" value=" x " onclick="update String('
        >* ')" />
        ><input type="button" name="div" value=" / " onclick="update String(' /
        >')" />
        ><input type="button" name="mod" value=" mod " onclick="update String('
        >% ')" /><br />
        ><input type="button" name="zero" value=" 0 " onclick="update String(' 0
        >')" />
        ><input type="button" name="one" value=" 1 " onclick="update String(' 1
        >')" />
        ><input type="button" name="two" value=" 2 " onclick="update String(' 2
        >')" />
        ><input type="button" name="three" value=" 3 " onclick="update String('
        >3 ')" />
        ><input type="button" name="four" value=" 4 " onclick="update String(' 4
        >')" /><br />
        ><input type="button" name="five" value=" 5 " onclick="update String(' 5
        >')" />
        ><input type="button" name="six" value=" 6 " onclick="update String(' 6
        >')" />
        ><input type="button" name="seven" value=" 7 " onclick="update String('
        >7 ')" />
        ><input type="button" name="eight" value=" 8 " onclick="update String('
        >8 ')" />
        ><input type="button" name="nine" value=" 9 " onclick="update String(' 9
        >')" />
        ><br />
        >
        ><!-- // the onclick event for the calc button performs the calculation
        >using the eval() function with the input string variable -->
        ><!-- // the calculated value is then assigned as the value of the
        >input text box -->
        >
        ><input type="button" name="point" value=" . " onclick="update String('
        >. ')" />
        ><input type="button" name="clear" value=" clear "
        >onclick="docum ent.Calculator. Input.value=''; inputString=''" />
        ><input type="button" name="calc" value=" = "
        >onclick="docum ent.Calculator. Input.value=eva l(inputString);
        >inputString='' " />
        >
        ></form>
        >
        ></body>
        >
        ></html>[/color]

        Comment

        Working...