all i get is a NaN...

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

    #1

    all i get is a NaN...

    newbie, learning some javascript...

    started with this external script:
    ---------------------------------------------

    var newNumber = document.form1. entry

    function shout()
    {
    alert(newNumber + 5)
    }

    ---------------------------------------------


    within this html:
    ---------------------------------------------

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Untitl ed Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript" src="calculator-0001.js"></script>

    </head>

    <body>
    <form name="form1" method="post" action="">
    <input type="text" name="entry">
    <input type="submit" name="addFive" value="+5" onClick="shout( )">
    </form>
    </body>
    </html>

    ---------------------------------------------

    was hoping to get an alert with whatever number i put in the text with 5
    added to it.

    all i get is an alert with NaN.


    Hopefully you can see where i'm going wrong??



    ...b..


  • Lee

    #2
    Re: all i get is a NaN...

    badstyle said:[color=blue]
    >
    >newbie, learning some javascript...
    >
    >started with this external script:
    >---------------------------------------------
    >
    > var newNumber = document.form1. entry[/color]

    var newNumber = document.form1. entry.value

    Comment

    • badstyle

      #3
      Re: all i get is a NaN...


      "Lee" <REM0VElbspamtr ap@cox.net> wrote in message
      news:ch7rep01cf l@drn.newsguy.c om...[color=blue]
      > badstyle said:[color=green]
      >>
      >>newbie, learning some javascript...
      >>
      >>started with this external script:
      >>---------------------------------------------
      >>
      >> var newNumber = document.form1. entry[/color]
      >
      > var newNumber = document.form1. entry.value
      >[/color]

      cool thanks! got it sorted ;)


      function shout()
      {
      var newNumber
      newNumber = (document.form1 .entry.value*1) +(5)
      alert("the new number is: "+ newNumber)
      }

      just working on trying to create a working calculator now, then on to more
      complex tasks...

      chars

      ...b..


      Comment

      • Mick White

        #4
        Re: all i get is a NaN...

        badstyle wrote:
        [color=blue]
        > "Lee" <REM0VElbspamtr ap@cox.net> wrote in message
        > news:ch7rep01cf l@drn.newsguy.c om...
        >[color=green]
        >>badstyle said:
        >>[color=darkred]
        >>>newbie, learning some javascript...
        >>>
        >>>started with this external script:
        >>>---------------------------------------------
        >>>
        >>>var newNumber = document.form1. entry[/color]
        >>
        >> var newNumber = document.form1. entry.value
        >>[/color]
        >
        >
        > cool thanks! got it sorted ;)
        >
        >
        > function shout()
        > {
        > var newNumber
        > newNumber = (document.form1 .entry.value*1) +(5)
        > alert("the new number is: "+ newNumber)
        > }
        >[/color]
        function shout(){
        alert("the new number is: "+(+document.fo rm1.entry.value +5))
        }
        You have used "newNumber" three times, when it is really not necessary
        to do so.
        And it is usually worthwhile to generalize your functions, if possible:

        function addAndShout(val ,addThis){
        alert("the new number is: "+(+val+addThis ))
        }

        Mick[color=blue]
        > just working on trying to create a working calculator now, then on to more
        > complex tasks...
        >
        > chars
        >
        > ..b..
        >
        >[/color]

        Comment

        • Dr John Stockton

          #5
          Re: all i get is a NaN...

          JRS: In article <ch84sp$8j3$1@h ercules.btinter net.com>, dated Thu, 2
          Sep 2004 21:55:37, seen in news:comp.lang. javascript, badstyle
          <spam@spam.co m> posted :[color=blue]
          >
          >function shout()
          >{
          >var newNumber
          >newNumber = (document.form1 .entry.value*1) +(5)
          >alert("the new number is: "+ newNumber)
          >}
          >[/color]


          function shout() { var newNumber
          newNumber = +document.form1 .entry.value + 5
          alert("The new number is: " + newNumber)
          }


          Code should be indented to show the intended structure, especially if
          you expect anyone else to read it.

          Your assignment line is unnecessarily complex.

          See posted newsgroup FAQ, & via below.

          BTW, using a blatantly false name is a technical newsgroup only makes
          you look silly; and you should never give an E-mail address that you do
          not have explicit permission to use.

          --
          © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
          <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
          <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
          <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

          Comment

          • badstyle

            #6
            Re: all i get is a NaN...

            "Dr John Stockton" <spam@merlyn.de mon.co.uk> wrote in message
            news:X94pTMAwkE OBFwOa@merlyn.d emon.co.uk...[color=blue]
            > JRS: In article <ch84sp$8j3$1@h ercules.btinter net.com>, dated Thu, 2
            > Sep 2004 21:55:37, seen in news:comp.lang. javascript, badstyle
            > <spam@spam.co m> posted :[color=green]
            >>
            >>function shout()
            >>{
            >>var newNumber
            >>newNumber = (document.form1 .entry.value*1) +(5)
            >>alert("the new number is: "+ newNumber)
            >>}
            >>[/color]
            >
            >
            > function shout() { var newNumber
            > newNumber = +document.form1 .entry.value + 5
            > alert("The new number is: " + newNumber)
            > }
            >
            >
            > Code should be indented to show the intended structure, especially if
            > you expect anyone else to read it.
            >
            > Your assignment line is unnecessarily complex.[/color]

            this is the very first sccript that i've attempted in javascript. It is
            important for me to understand the processing of the information in my own
            way first hence why it lacks efficiency too. "Correct" and a more refined
            structure will come later the better and more proficient i become at
            understanding the language and it's syntax. I can only do this with help
            from people like you who are willing to point this out, so thank you but...
            [color=blue]
            > See posted newsgroup FAQ, & via below.[/color]

            what am i looking for specifically within this??
            [color=blue]
            > BTW, using a blatantly false name is a technical newsgroup only makes
            > you look silly; and you should never give an E-mail address that you do
            > not have explicit permission to use.
            >[/color]

            in response to your statements:

            1. i reserve my right to have real world personally identifiable information
            kept to my self. even if i did change the "name" to 'bob cratchet' or
            'stanley johnson' if my name was actually 'bob crachet' or 'stanley johnson'
            it would still make no difference to the question being asked. lets try and
            not get petty about very insignificant matters as "real" or false
            identities.

            In truth real identities are generally not important on the internet unless
            you are making financial transations. As shakespeare once meant to state:
            "What's in a name? That which we call a badstyle by any other name would
            still be crap at javascript"

            instead he famously stated: "What's in a name? That which we call a rose by
            any other word would smell as sweet.".

            2. point taken. the email is set to that so as not to recieve spam via mail
            bots - in this day in age, a responsible move. unfortunately i stupidly used
            an address i was not responsible for. The last email account i set up to use
            on newsgroups became unusable due to this fact, so i've set up a real one to
            catter for this inconvenience.




            ...b..



            [color=blue]
            > © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4
            > ©
            > <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of
            > news:comp.lang. javascript
            > <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates,
            > sources.
            > <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items,
            > links.[/color]


            Comment

            • badstyle

              #7
              Re: all i get is a NaN...


              "Mick White" <mwhite13@BOGUS rochester.rr.co m> wrote in message
              news:dYMZc.5355 8$Kt5.28501@twi ster.nyroc.rr.c om...[color=blue]
              > badstyle wrote:
              >[color=green]
              >> "Lee" <REM0VElbspamtr ap@cox.net> wrote in message
              >> news:ch7rep01cf l@drn.newsguy.c om...
              >>[color=darkred]
              >>>badstyle said:
              >>>
              >>>>newbie, learning some javascript...
              >>>>
              >>>>started with this external script:
              >>>>---------------------------------------------
              >>>>
              >>>>var newNumber = document.form1. entry
              >>>
              >>> var newNumber = document.form1. entry.value
              >>>[/color]
              >>
              >>
              >> cool thanks! got it sorted ;)
              >>
              >>
              >> function shout()
              >> {
              >> var newNumber
              >> newNumber = (document.form1 .entry.value*1) +(5)
              >> alert("the new number is: "+ newNumber)
              >> }
              >>[/color]
              > function shout(){
              > alert("the new number is: "+(+document.fo rm1.entry.value +5))
              > }
              > You have used "newNumber" three times, when it is really not necessary to
              > do so.
              > And it is usually worthwhile to generalize your functions, if possible:
              >
              > function addAndShout(val ,addThis){
              > alert("the new number is: "+(+val+addThis ))
              > }
              >
              > Mick[/color]

              interesting, how come the string "document.form1 .entry.value" didn't need to
              be turned into an interger by multiplying the value by 1?

              I had originally tried a version like that which didn't work, is it the '+'
              prior to the string which allows the value to be accepted as the interger?


              ...b..


              Comment

              • Richard Cornford

                #8
                Re: all i get is a NaN...

                badstyle wrote:[color=blue]
                > "Mick White wrote:[color=green]
                >> function shout(){
                >> alert("the new number is: "+(+document.fo rm1.entry.value +5))
                >> }
                >> You have used "newNumber" three times, when it
                >> is really not necessary to do so.
                >> And it is usually worthwhile to generalize your
                >> functions, if possible:
                >>
                >> function addAndShout(val ,addThis){
                >> alert("the new number is: "+(+val+addThis ))
                >> }
                >>
                >> Mick[/color]
                >
                > interesting, how come the string "document.form1 .entry.value"
                > didn't need to be turned into an interger by multiplying the
                > value by 1?[/color]

                The multiplication of a number-like string value by one is just one of
                many ways of forcing type-conversion to a number. The subtraction of
                zero, for example, achieves exactly the same result. In this case Mick
                was using the unary plus operator to force the type-conversion.
                [color=blue]
                > I had originally tried a version like that which didn't work,
                > is it the '+' prior to the string which allows the value to
                > be accepted as the interger?[/color]

                RTFM?

                The expression - +"12" - is evaluated as the number 12 as the unary plus
                operator type-converts its operand into a number (type). Unary plus is
                the fastest method of type-converting a string into a number as it does
                not involve any additional mathematical operations. Its major drawback
                is that it is not particularly clear when it appears in source code
                (especially when mixed in statements containing addition or
                concatenation operations). So where Mick wrote:-

                (+document.form 1.entry.value+5 )

                - I would have written:-

                ((+document.for m1.entry.value) + 5)

                - parenthesising the unary plus expression so that it was clear that was
                unrelated to the following addition (or the preceding concatenation).

                Where source code clarity is more important than speed of execution
                type-converting to a number is best done by using the Number constructor
                as a function:-

                ( Number(document .form1.entry.va lue) + 5)

                It is about 4 times slower than the (optimum) unary plus operator at
                performing the type-conversion but virtually self-documenting.

                With unary plus offering the ultimate in speed and the Number
                constructor (called as a function) offering the ultimate in clarity,
                there is no good reason to be using any other type-converting-to-number
                methods when writing javascript. (non-type-converting parseInt and
                parseFloat have their own place and applications.)

                Richard.


                Comment

                • Dr John Stockton

                  #9
                  Re: all i get is a NaN...

                  JRS: In article <chc723$4f7$1@h ercules.btinter net.com>, dated Sat, 4
                  Sep 2004 10:57:07, seen in news:comp.lang. javascript, badstyle <phatwap-
                  spam01@yahoo.co .uk> posted :[color=blue]
                  >
                  >[color=green]
                  >> See posted newsgroup FAQ, & via below.[/color]
                  >
                  >what am i looking for specifically within this??[/color]

                  Everything. It will save your time and it will save our time. Read not
                  only what is cited, but all links pertinent to your interests and
                  intended actions.

                  [color=blue][color=green]
                  >> BTW, using a blatantly false name is a technical newsgroup only makes
                  >> you look silly; and you should never give an E-mail address that you do
                  >> not have explicit permission to use.
                  >>[/color]
                  >
                  >in response to your statements:
                  >
                  >1. i reserve my right to have real world personally identifiable information
                  >kept to my self. even if i did change the "name" to 'bob cratchet' or
                  >'stanley johnson' if my name was actually 'bob crachet' or 'stanley johnson'
                  >it would still make no difference to the question being asked. lets try and
                  >not get petty about very insignificant matters as "real" or false
                  >identities.[/color]


                  Thos who give real identities are more respected than those who do not,
                  on the average and with few exceptions. There is nothing to stop you
                  from using a consistent, plausible identity. Your present one merely
                  proclaims your immaturity, as does your slovenly typing style.

                  [color=blue][color=green]
                  >> © John[/color][/color]

                  Don't quote signatures, unless discussing them,

                  --
                  © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
                  Web <URL:http://www.merlyn.demo n.co.uk/> - FAQish topics, acronyms, & links.
                  Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
                  Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)

                  Comment

                  Working...