why math wont work?

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

    why math wont work?

    sorry for the simple question, haven't done this in a while. when I use the
    following script it keeps displaying the value of "x" like a string. for
    example, if I type the number 7 in the prompt, it displays the result as 721
    instead of the answer I want which is 28. what am I doing wrong.
    hanks -Allen Thompson

    <html>
    <body>
    <script language="javas cript">
    var y=window.prompt ();
    var x=y+3*7;
    document.write( x);
    </script>
    </body>
    </html>


  • Fabian

    #2
    Re: why math wont work?

    Allen Thompson hu kiteb:
    [color=blue]
    > sorry for the simple question, haven't done this in a while. when I
    > use the following script it keeps displaying the value of "x" like a
    > string. for example, if I type the number 7 in the prompt, it
    > displays the result as 721 instead of the answer I want which is 28.
    > what am I doing wrong.
    > hanks -Allen Thompson
    >
    > <html>
    > <body>
    > <script language="javas cript">
    > var y=window.prompt ();
    > var x=y+3*7;[/color]

    var x = (y * 1) + (3*7);

    Unless you force js to recognise a variable as a number, half teh time
    it assumes it is text, and teh + operator is both addition and
    concatenation.

    If I had my way, + would be reserved as addition only, and a separate
    operator created for concatenation.


    --
    --
    Fabian
    Visit my website often and for long periods!
    AGAM69 menghadirkan inspirasi desain kreatif, solusi digital, pengembangan teknologi, serta inovasi modern untuk kebutuhan bisnis dan profesional.


    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: why math wont work?

      "Fabian" <lajzar@hotmail .com> writes:
      [color=blue]
      > var x = (y * 1) + (3*7);[/color]

      Or
      var x = Number(y)+3*7
      or one of the other ways to convert a string into a number.

      The important point is that y contains a string (prompt returns a
      string), not a number, and that adding a number to a string will
      always convert the number to a second string and concatenate the
      strings.

      <URL:http://jibbering.com/faq/#FAQ4_21>
      [color=blue]
      > Unless you force js to recognise a variable as a number, half teh time
      > it assumes it is text, and teh + operator is both addition and
      > concatenation.[/color]

      It's not *that* bad. Javascript won't assume something half of the time.
      It consistently treats strings as strings and numbers as numbers, and
      when adding something to a string, it converts the other part to a string
      and concatenates.

      I would rather say that half of the time, you get away with forgetting
      that you have a string containing a numeral, because Javascript converts
      it to a number before doing arithmetic operations on it. E.g.,
      var y = "42";
      var x1 = y * 2; // 84
      var x2 = y / 2; // 21
      var x3 = y - 37; // 5
      var x4 = y + 10; // 4210 - addition doesn't convert arguments to numbers.
      In the first four cases, you can safely forget that y contains a string.
      It's just that you can't always, so you shouldn't.
      [color=blue]
      > If I had my way, + would be reserved as addition only, and a separate
      > operator created for concatenation.[/color]

      I wouldn't complain about that. I guess the behavior is stolen from Java,
      where + also does concatenation on strings.
      /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

      • Douglas Crockford

        #4
        Re: why math wont work?

        > when I use the[color=blue]
        > following script it keeps displaying the value of "x" like a string. for
        > example, if I type the number 7 in the prompt, it displays the result as 721
        > instead of the answer I want which is 28. what am I doing wrong.
        > hanks -Allen Thompson
        >
        > <html>
        > <body>
        > <script>
        > var y=window.prompt ();
        > var x=y+3*7;
        > document.write( x);
        > </script>
        > </body>
        > </html>[/color]

        prompt() returns a string. Before using the '+' operator, you need to convert
        the string to a number.

        var x = (+y) + 3 * 7;



        Comment

        • Dennis M. Marks

          #5
          Re: why math wont work?

          In article <bre3sh$2di3h$1 @ID-174912.news.uni-berlin.de>, Fabian
          <lajzar@hotmail .com> wrote:
          [color=blue]
          > Allen Thompson hu kiteb:
          >[color=green]
          > > sorry for the simple question, haven't done this in a while. when I
          > > use the following script it keeps displaying the value of "x" like a
          > > string. for example, if I type the number 7 in the prompt, it
          > > displays the result as 721 instead of the answer I want which is 28.
          > > what am I doing wrong.
          > > hanks -Allen Thompson
          > >
          > > <html>
          > > <body>
          > > <script language="javas cript">
          > > var y=window.prompt ();
          > > var x=y+3*7;[/color]
          >
          > var x = (y * 1) + (3*7);
          >[/color]
          <snip>

          Just use var x=+y+3*7;

          If the y was at the other end of the formula then
          var x=3*7+(+y)

          --
          Dennis M. Marks


          -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
          http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
          -----== Over 100,000 Newsgroups - 19 Different Servers! =-----

          Comment

          • Dr John Stockton

            #6
            Re: why math wont work?

            JRS: In article <esvCb.43$im.42 @newsread2.news .atl.earthlink. net>, seen
            in news:comp.lang. javascript, Allen Thompson <genericjoe@min dspring.com>
            posted at Sat, 13 Dec 2003 03:12:10 :-[color=blue]
            >sorry for the simple question, haven't done this in a while. when I use the
            >following script it keeps displaying the value of "x" like a string. for
            >example, if I type the number 7 in the prompt, it displays the result as 721
            >instead of the answer I want which is 28. what am I doing wrong.[/color]

            You basic error lies in omitting to read the newsgroup FAQ.

            --
            © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
            <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
            <URL:http://www.merlyn.demo n.co.uk/js-index.htm> Jsc maths, dates, sources.
            <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/Jsc/&c, FAQ topics, links.

            Comment

            • Thomas 'PointedEars' Lahn

              #7
              Re: why math wont work?

              Dennis M. Marks wrote:
              [color=blue]
              > If the y was at the other end of the formula then
              > var x=3*7+(+y)[/color]

              JFTR:

              var x = 3*7+ +y;

              is also possible but more error-catching.


              PointedEars

              Comment

              • Thomas 'PointedEars' Lahn

                #8
                Re: why math wont work?

                Douglas Crockford wrote:
                [color=blue]
                > prompt() returns a string.[/color]

                It returns a string if the user confirms. Otherwise
                it returns `null', no matter what has been typed.


                PointedEars

                Comment

                • Lasse Reichstein Nielsen

                  #9
                  Re: why math wont work?

                  Thomas 'PointedEars' Lahn <PointedEars@we b.de> writes:
                  [color=blue]
                  > Douglas Crockford wrote:
                  >[color=green]
                  >> prompt() returns a string.[/color]
                  >
                  > It returns a string if the user confirms. Otherwise
                  > it returns `null', no matter what has been typed.[/color]

                  That is browser dependent. Opera returns "undefined" , not "null".
                  Bot IE and Mozilla do return "null".

                  /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

                  • Dauber!

                    #10
                    Re: why math wont work?

                    Waaaaay back on 13-Dec-03 11:26:28, Douglas Crockford said this about Re: why math wont work?:
                    [color=blue][color=green]
                    >> following script it keeps displaying the value of "x" like a string. for
                    >> example, if I type the number 7 in the prompt, it displays the result as
                    >> 721 instead of the answer I want which is 28. what am I doing wrong. hanks
                    >> -Allen Thompson
                    >>
                    >> <html>
                    >> <body>
                    >> <script>
                    >> var y=window.prompt ();
                    >> var x=y+3*7;
                    >> document.write( x);[/color][/color]
                    [color=blue]
                    >prompt() returns a string. Before using the '+' operator, you need to convert
                    >the string to a number.[/color]
                    [color=blue]
                    > var x = (+y) + 3 * 7;[/color]

                    That'll still give the same result, unfortunately. Whatcha need to do is this;

                    var x = parseInt(y)+3*7 ;

                    That should work.

                    --
                    dauber@banana-and-louie.org * dauber.50megs.c om
                    * ICQ: 28677921 * YIM: dau_ber * AIM: ddaauubbeerr

                    Comment

                    • Thomas 'PointedEars' Lahn

                      #11
                      Re: why math wont work?

                      Lasse Reichstein Nielsen wrote:
                      [color=blue]
                      > Thomas 'PointedEars' Lahn <PointedEars@we b.de> writes:[color=green]
                      >> Douglas Crockford wrote:[color=darkred]
                      >>> prompt() returns a string.[/color]
                      >>
                      >> It returns a string if the user confirms. Otherwise
                      >> it returns `null', no matter what has been typed.[/color]
                      >
                      > That is browser dependent. Opera returns "undefined" , not "null".[/color]

                      *Are* *you* *really* *really* *sure*? [psf 1.1]

                      My Opera/7.11 (Windows NT 5.0; U) [en] returns `null', too.

                      Done the following tests in the Location Bar:

                      javascript:aler t(prompt("bla") ) // `null' if canceled
                      javascript:aler t(typeof prompt("bla")) // `object' if canceled


                      PointedEars

                      Comment

                      • Thomas 'PointedEars' Lahn

                        #12
                        Re: why math wont work?

                        Dauber! wrote:[color=blue]
                        > Waaaaay back on 13-Dec-03 11:26:28, Douglas Crockford said this about Re: why math wont work?:[/color]

                        The recommended, if not even standardized, maximum is 78 characters
                        per line. Besides, about 74.7% of your attribution is composed of
                        superfluous information.
                        [color=blue][color=green]
                        >>prompt() returns a string. Before using the '+' operator, you need to convert
                        >>the string to a number.[/color]
                        >[color=green]
                        >> var x = (+y) + 3 * 7;[/color]
                        >
                        > That'll still give the same result, unfortunately.[/color]

                        Not in my UAs. But I think you do not
                        want to be taken seriously, so go away.
                        [color=blue]
                        > Whatcha need to do is this;
                        >
                        > var x = parseInt(y)+3*7 ;
                        >
                        > That should work.[/color]

                        See the FAQ about parseInt(...).

                        And of course it will work, but the above
                        is about 6 times faster and more reliable.
                        [color=blue]
                        > --[/color]

                        The trailing space is missing.


                        PointedEars

                        Comment

                        • HikksNotAtHome

                          #13
                          Re: why math wont work?

                          In article <807.478T2962T1 0846128dau_ber@ verizon.SPAMISF OREATING.net.in valid>,
                          "Dauber!" <dau_ber@verizo n.SPAMISFOREATI NG.net.invalid> writes:
                          [color=blue][color=green]
                          >> var x = (+y) + 3 * 7;[/color]
                          >
                          >That'll still give the same result, unfortunately. Whatcha need to do is
                          >this;[/color]

                          Did you bother testing that before spouting off that it would give the same
                          results?
                          It is *well* known in this group that +y where y is a number in a string format
                          is quicker at converting it to a number than parseInt is, which you use
                          incorrectly.

                          [color=blue]
                          >var x = parseInt(y)+3*7 ;
                          >
                          >That should work.[/color]

                          As long as y doesn't start with a 0 or 0x format.



                          --
                          Randy

                          Comment

                          • Lasse Reichstein Nielsen

                            #14
                            Re: why math wont work?

                            Thomas 'PointedEars' Lahn <PointedEars@we b.de> writes:
                            [color=blue]
                            > *Are* *you* *really* *really* *sure*? [psf 1.1][/color]

                            *Yes* *I* *am*!
                            [color=blue]
                            > My Opera/7.11 (Windows NT 5.0; U) [en] returns `null', too.[/color]

                            My Opera/7.23 (Windows NT 5.1; U) [en] returns 'undefined'
                            [color=blue]
                            > javascript:aler t(typeof prompt("bla")) // `object' if canceled[/color]

                            on this exact test.

                            /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

                            • Dauber!

                              #15
                              Re: why math wont work?

                              Waaaaay back on 14-Dec-03 19:16:53, HikksNotAtHome said this about Re: why math wont work?:
                              [color=blue][color=green][color=darkred]
                              >>> var x = (+y) + 3 * 7;[/color]
                              >>
                              >>That'll still give the same result, unfortunately. Whatcha need to do is
                              >>this;[/color][/color]
                              [color=blue]
                              >Did you bother testing that before spouting off that it would give the same
                              >results?[/color]

                              Uhhh....yes. Believe it or not, despite popular belief, not all browsers
                              interpret JavaScript the same way, including two of the ones I use.
                              [color=blue]
                              >It is *well* known in this group that +y where y is a number in a string
                              >format is quicker at converting it to a number than parseInt is, which you
                              >use incorrectly.[/color]

                              Funny...two references I checked, including one that helped me get an A in
                              my JavaScript programming class that I took for my webmaster admin
                              certification, indicates that it's perfectly valid; in fact, I'm given
                              several overloaded versions tht account for "0x" format as well as binary
                              and octal...

                              --
                              dauber@banana-and-louie.org
                              * ICQ: 28677921 * YIM: dau_ber * AIM: ddaauubbeerr

                              Comment

                              Working...