Strings and numbers...?

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

    Strings and numbers...?

    I'm having difficulties arithmetically manipulating form element values.

    I've entered data into the form, and I fetch them using a js, as:

    p7Left = Number(document .form1.elements["p7_left"].value);
    p7Right = Number(document .form1.elements["p7_right"].value);
    ...
    ...
    scoreLeft = Number(document .form1.elements["left_score "].value);
    scoreRight = Number(document .form1.elements["right_scor e"].value);

    Then I add these as follows:

    scoreLeft = Number(scoreLef t + p&Left + ...... +);

    This does what I want; without the operator 'Number' I get a concatination
    of the various variables (as expected). Is there some way of globally
    defining all variables as numbers instead of strings?

    --
    Ed Jay (remove M to respond by email)
  • Matt Kruse

    #2
    Re: Strings and numbers...?

    Ed Jay wrote:[color=blue]
    > This does what I want; without the operator 'Number' I get a
    > concatination of the various variables (as expected). Is there some
    > way of globally defining all variables as numbers instead of strings?[/color]

    No.

    But you should see http://www.JavascriptToolbox.com/bestpractices/ for an
    entry about this topic and the best way to solve it.

    --
    Matt Kruse




    Comment

    • Ed Jay

      #3
      Re: Strings and numbers...?

      "Matt Kruse" <newsgroups@mat tkruse.com> wrote:
      [color=blue]
      >Ed Jay wrote:[color=green]
      >> This does what I want; without the operator 'Number' I get a
      >> concatination of the various variables (as expected). Is there some
      >> way of globally defining all variables as numbers instead of strings?[/color]
      >
      >No.
      >
      >But you should see http://www.JavascriptToolbox.com/bestpractices/ for an
      >entry about this topic and the best way to solve it.[/color]

      Thanks.

      --
      Ed Jay (remove M to respond by email)

      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: Strings and numbers...?

        Ed Jay <edMbj@aes-intl.com> writes:
        [color=blue]
        > I'm having difficulties arithmetically manipulating form element values.[/color]
        ....[color=blue]
        > scoreRight = Number(document .form1.elements["right_scor e"].value);[/color]
        ....[color=blue]
        > scoreLeft = Number(scoreLef t + p&Left + ...... +);
        >
        > This does what I want; without the operator 'Number' I get a concatination
        > of the various variables (as expected). Is there some way of globally
        > defining all variables as numbers instead of strings?[/color]

        No.
        In the current version of Javascript used in browsers, variables have no
        type. Only values have type. If you put a string into a variable, that
        is also what you get out.

        You are doing the right thing in converting form control values to
        numbers. You should then remember what types your values contain.
        The result of adding the numbers from the first variables is already
        a number, so you don't need to convert it again before assingning to
        scoreLeft.

        Good luck
        /L
        --
        Lasse Reichstein Nielsen - lrn@hotpop.com
        DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        • Ed Jay

          #5
          Re: Strings and numbers...?

          Lasse Reichstein Nielsen <lrn@hotpop.com > wrote:
          [color=blue]
          >Ed Jay <edMbj@aes-intl.com> writes:
          >[color=green]
          >> I'm having difficulties arithmetically manipulating form element values.[/color]
          >...[color=green]
          >> scoreRight = Number(document .form1.elements["right_scor e"].value);[/color]
          >...[color=green]
          >> scoreLeft = Number(scoreLef t + p&Left + ...... +);
          >>
          >> This does what I want; without the operator 'Number' I get a concatination
          >> of the various variables (as expected). Is there some way of globally
          >> defining all variables as numbers instead of strings?[/color]
          >
          >No.
          >In the current version of Javascript used in browsers, variables have no
          >type. Only values have type. If you put a string into a variable, that
          >is also what you get out.
          >
          >You are doing the right thing in converting form control values to
          >numbers. You should then remember what types your values contain.
          >The result of adding the numbers from the first variables is already
          >a number, so you don't need to convert it again before assingning to
          >scoreLeft.
          >
          >Good luck
          >[/color]
          Thanks, Lasse.

          --
          Ed Jay (remove M to respond by email)

          Comment

          • Dr John Stockton

            #6
            Re: Strings and numbers...?

            JRS: In article <tdc7u1dbrpsg5j mu4sffk858sv0te d8c89@4ax.com>, dated
            Fri, 3 Feb 2006 11:48:02 remote, seen in news:comp.lang. javascript, Ed
            Jay <edMbj@aes-intl.com> posted :[color=blue]
            >I'm having difficulties arithmetically manipulating form element values.
            >
            >I've entered data into the form, and I fetch them using a js, as:
            >
            >p7Left = Number(document .form1.elements["p7_left"].value);
            >p7Right = Number(document .form1.elements["p7_right"].value);
            >..
            >..
            >scoreLeft = Number(document .form1.elements["left_score "].value);
            >scoreRight = Number(document .form1.elements["right_scor e"].value);
            >
            >Then I add these as follows:
            >
            >scoreLeft = Number(scoreLef t + p&Left + ...... +);
            >
            >This does what I want; without the operator 'Number' I get a concatination
            >of the various variables (as expected). Is there some way of globally
            >defining all variables as numbers instead of strings?[/color]

            You should try reading the newsgroup FAQ; the indexing for your question
            is obvious, leading to 4.21.

            Rather than Number(...) you can best use +... .

            BTW, & != 7 .

            YSCIB.

            --
            © 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

            • Ed Jay

              #7
              Re: Strings and numbers...?

              Dr John Stockton <jrs@merlyn.dem on.co.uk> wrote:
              [color=blue]
              >JRS: In article <tdc7u1dbrpsg5j mu4sffk858sv0te d8c89@4ax.com>, dated
              >Fri, 3 Feb 2006 11:48:02 remote, seen in news:comp.lang. javascript, Ed
              >Jay <edMbj@aes-intl.com> posted :[color=green]
              >>I'm having difficulties arithmetically manipulating form element values.
              >>
              >>I've entered data into the form, and I fetch them using a js, as:
              >>
              >>p7Left = Number(document .form1.elements["p7_left"].value);
              >>p7Right = Number(document .form1.elements["p7_right"].value);
              >>..
              >>..
              >>scoreLeft = Number(document .form1.elements["left_score "].value);
              >>scoreRight = Number(document .form1.elements["right_scor e"].value);
              >>
              >>Then I add these as follows:
              >>
              >>scoreLeft = Number(scoreLef t + p&Left + ...... +);
              >>
              >>This does what I want; without the operator 'Number' I get a concatination
              >>of the various variables (as expected). Is there some way of globally
              >>defining all variables as numbers instead of strings?[/color]
              >
              >You should try reading the newsgroup FAQ; the indexing for your question
              >is obvious, leading to 4.21.
              >
              >Rather than Number(...) you can best use +... .
              >
              >BTW, & != 7 .[/color]

              You have good eyes. :-)[color=blue]
              >
              >YSCIB.
              >[/color]
              Thanks. I ended up subtracting '0' from every value.

              --
              Ed Jay (remove M to respond by email)

              Comment

              • Randy Webb

                #8
                Re: Strings and numbers...?

                Ed Jay said the following on 2/4/2006 6:00 PM:[color=blue]
                > Dr John Stockton <jrs@merlyn.dem on.co.uk> wrote:
                >[color=green]
                >> JRS: In article <tdc7u1dbrpsg5j mu4sffk858sv0te d8c89@4ax.com>, dated
                >> Fri, 3 Feb 2006 11:48:02 remote, seen in news:comp.lang. javascript, Ed
                >> Jay <edMbj@aes-intl.com> posted :[color=darkred]
                >>> I'm having difficulties arithmetically manipulating form element values.
                >>>
                >>> I've entered data into the form, and I fetch them using a js, as:
                >>>
                >>> p7Left = Number(document .form1.elements["p7_left"].value);
                >>> p7Right = Number(document .form1.elements["p7_right"].value);
                >>> ..
                >>> ..
                >>> scoreLeft = Number(document .form1.elements["left_score "].value);
                >>> scoreRight = Number(document .form1.elements["right_scor e"].value);
                >>>
                >>> Then I add these as follows:
                >>>
                >>> scoreLeft = Number(scoreLef t + p&Left + ...... +);
                >>>
                >>> This does what I want; without the operator 'Number' I get a concatination
                >>> of the various variables (as expected). Is there some way of globally
                >>> defining all variables as numbers instead of strings?[/color]
                >> You should try reading the newsgroup FAQ; the indexing for your question
                >> is obvious, leading to 4.21.
                >>
                >> Rather than Number(...) you can best use +... .
                >>
                >> BTW, & != 7 .[/color]
                >
                > You have good eyes. :-)[color=green]
                >> YSCIB.
                >>[/color]
                > Thanks. I ended up subtracting '0' from every value.
                >[/color]

                scoreLeft = +document.form1 .elements["left_score "].value;

                Is more efficient than subtracting zero.

                --
                Randy
                comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
                Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

                Comment

                • Ed Jay

                  #9
                  Re: Strings and numbers...?

                  Randy Webb <HikksNotAtHome @aol.com> wrote:
                  [color=blue]
                  >Ed Jay said the following on 2/4/2006 6:00 PM:[color=green]
                  >> Dr John Stockton <jrs@merlyn.dem on.co.uk> wrote:
                  >>[color=darkred]
                  >>> JRS: In article <tdc7u1dbrpsg5j mu4sffk858sv0te d8c89@4ax.com>, dated
                  >>> Fri, 3 Feb 2006 11:48:02 remote, seen in news:comp.lang. javascript, Ed
                  >>> Jay <edMbj@aes-intl.com> posted :
                  >>>> I'm having difficulties arithmetically manipulating form element values.
                  >>>>
                  >>>> I've entered data into the form, and I fetch them using a js, as:
                  >>>>
                  >>>> p7Left = Number(document .form1.elements["p7_left"].value);
                  >>>> p7Right = Number(document .form1.elements["p7_right"].value);
                  >>>> ..
                  >>>> ..
                  >>>> scoreLeft = Number(document .form1.elements["left_score "].value);
                  >>>> scoreRight = Number(document .form1.elements["right_scor e"].value);
                  >>>>
                  >>>> Then I add these as follows:
                  >>>>
                  >>>> scoreLeft = Number(scoreLef t + p&Left + ...... +);
                  >>>>
                  >>>> This does what I want; without the operator 'Number' I get a concatination
                  >>>> of the various variables (as expected). Is there some way of globally
                  >>>> defining all variables as numbers instead of strings?
                  >>> You should try reading the newsgroup FAQ; the indexing for your question
                  >>> is obvious, leading to 4.21.
                  >>>
                  >>> Rather than Number(...) you can best use +... .
                  >>>
                  >>> BTW, & != 7 .[/color]
                  >>
                  >> You have good eyes. :-)[color=darkred]
                  >>> YSCIB.
                  >>>[/color]
                  >> Thanks. I ended up subtracting '0' from every value.
                  >>[/color]
                  >
                  >scoreLeft = +document.form1 .elements["left_score "].value;
                  >
                  >Is more efficient than subtracting zero.[/color]

                  Thank you.

                  --
                  Ed Jay (remove M to respond by email)

                  Comment

                  • Dr John Stockton

                    #10
                    Re: Strings and numbers...?

                    JRS: In article <eecau15k9pf3ui n4ebq2i53hqokrt 6sa0c@4ax.com>, dated
                    Sat, 4 Feb 2006 15:00:00 remote, seen in news:comp.lang. javascript, Ed
                    Jay <edMbj@aes-intl.com> posted :[color=blue]
                    >Thanks. I ended up subtracting '0' from every value.
                    >[/color]

                    You should read the FAQ more carefully; there is no need to subtract
                    zero, and your solution uses twice as many characters as is necessary.
                    The matter is also dealt with on my Web site.

                    --
                    © 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

                    • Ed Jay

                      #11
                      Re: Strings and numbers...?

                      Dr John Stockton <jrs@merlyn.dem on.co.uk> wrote:
                      [color=blue]
                      >JRS: In article <eecau15k9pf3ui n4ebq2i53hqokrt 6sa0c@4ax.com>, dated
                      >Sat, 4 Feb 2006 15:00:00 remote, seen in news:comp.lang. javascript, Ed
                      >Jay <edMbj@aes-intl.com> posted :[color=green]
                      >>Thanks. I ended up subtracting '0' from every value.
                      >>[/color]
                      >
                      >You should read the FAQ more carefully;[/color]

                      I agree.
                      [color=blue]
                      > there is no need to subtract
                      >zero, and your solution uses twice as many characters as is necessary.
                      >The matter is also dealt with on my Web site.
                      >[/color]
                      I ended up following Randy's suggestion and put + before each value.

                      --
                      Ed Jay (remove M to respond by email)

                      Comment

                      • Matt Kruse

                        #12
                        Re: Strings and numbers...?

                        Dr John Stockton wrote:[color=blue]
                        > there is no need to subtract
                        > zero, and your solution uses twice as many characters as is necessary.[/color]

                        Are you really using that as an argument? :)

                        --
                        Matt Kruse




                        Comment

                        Working...