SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Dec 2013
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    js prompt - does not function just with +???

    Helo!
    I wrote this:

    Code:
    <!DOCTYPE html>
    <html>
    <body>
    
    <p>Click the button to demonstrate the prompt box.</p>
    
    <button onclick="myFunction()">Try it</button>
    
    <p id="demo"></p>
    
    <script>
    function myFunction()
    {
    var x;
    
    var number=prompt("nb","nbpl?");
    
    if (number!=0)
      {
      x=number+2;
      document.getElementById("demo").innerHTML=x;
      }
    }
    </script>
    
    </body>
    </html>
    It works with: number/2, number*2, number-2, but just not with number+2.

    Please, any hints how to solve the problem?
    Many thanks!
    Last edited by Pullo; Dec 19, 2013 at 10:44. Reason: Added code tags

  2. #2
    The CSS Clinic is open silver trophybronze trophy
    Paul O'B's Avatar
    Join Date
    Jan 2003
    Location
    Hampshire UK
    Posts
    39,784
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    The problem arises from the dual nature of the + operator used for both numeric addition and string concatenation. If both operands are numbers to start with will the + operator performs addition otherwise it converts all of its operands to strings and does concatenation.

    Therefore 2 + 2 = 22

    Convert the string to a number with parseInt (for integers) or ParseFloat (for floating point numbers).

    e.g.

    x=parseInt(number)+2;

    More info here and here.

  3. #3
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,312
    Mentioned
    177 Post(s)
    Tagged
    8 Thread(s)
    In addition to what Paul says, note that the parseInt function takes an optional second parameter (radix) that specifies which numeral system is to be used.
    For example, a radix of 16 (hexadecimal) indicates that the number in the string should be parsed from a hexadecimal number to a decimal number.

    If the radix parameter is omitted, JavaScript assumes the following:

    If the string begins with "0x", the radix is 16 (hexadecimal)
    If the string begins with "0", the radix is 8 (octal). This feature is deprecated
    If the string begins with any other value, the radix is 10 (decimal)[/QUOTE]

    Ref: http://stackoverflow.com/questions/4...-in-javascript

    If your string is already in the form of a number, it might be better to use Number() which converts the object argument to a number that represents the object's value.
    If the value cannot be converted to a legal number, NaN is returned.

    An interesting shorthand for this is the unary plus: http://niki4810.github.io/blog/2013/...in-javascript/

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,597
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    The correct JavaScript function for converting a string to a number is

    Code:
    num = Number(str)
    or you can use the short version

    Code:
    num = (+str)
    note that to use the short version you must wrap the parentheses around the + and the string so that it operates as a unary + - which always converts to a number).
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Member
    Join Date
    Dec 2013
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    "Next line" in prompt() ???

    Hello!!!


    Please, how to get each result in other line? LIke:
    10
    2

    p.s. I tried with \ and with <br> but no result...

    Code is here:

    HTML Code:
    <!DOCTYPE html>
    <html>
    <body>
    
    <p>Click the button to demonstrate the prompt box.</p>
    
    <button onclick="myFunction()">Try it</button>
    
    <p id="demo"></p>
    
    <script>
    function myFunction()
    {
    var x;
    
    var nr=prompt("Please enter number","number1");
    var nr2=prompt("Please enter number","number2");
    
    
    
    
      
      x=parseInt(nr)+parseInt(nr2)+nr-nr2;
    
      document.getElementById("demo").innerHTML=x;
      
    }
    </script>
    
    </body>
    </html>
    Last edited by Mittineague; Dec 21, 2013 at 05:44. Reason: reformatting bbcode tags

  6. #6


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •