SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot 8Observer8's Avatar
    Join Date
    Jul 2013
    Location
    Russia
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    The wrong result

    Hi

    I enter 4 and 3. The perimeter will have 14. But I see 86. Why?

    Code:
    // Calculation of the perimeter box
    var perimeterBox = function(height, width) {
        return 2*(height+width);
    };
    
    // Input data
    var height = prompt("Enter a height value:", "Write a height value here");
    var width = prompt("Enter a width value:", "Write a width value here");
    
    // Processing data
    var parimeter = perimeterBox(height, width);
    
    // Output data
    confirm("The perimeter of a box (with: height = " + height +
            " and width = " + width + ") is " + perimeter);
    Thank you!

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,003
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    Because the values being passed to your perimiterBox() function are both strings.
    To perform the calculation you'll have to convert them to numbers, e.g.

    Code JavaScript:
    var perimeterBox = function(height, width) {
      return 2 * ( Number( height ) + Number( width ) );
    };

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,862
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    or a slightly shorter way of converting the strings to numbers:

    Code:
    var perimeterBox = function(height, width) {
      return 2 * ( (+height ) + (+width ) );
    };
    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="^$">

  4. #4
    SitePoint Zealot 8Observer8's Avatar
    Join Date
    Jul 2013
    Location
    Russia
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much, guys!

    It works too:

    Code:
    var perimeterBox = function(height, width) {
        return 2*(parseFloat(height)+parseFloat(width));
    };

  5. #5
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,003
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi felgall,

    Quote Originally Posted by felgall View Post
    or a slightly shorter way of converting the strings to numbers:

    Code:
    var perimeterBox = function(height, width) {
      return 2 * ( (+height ) + (+width ) );
    };
    That's a unary plus, right?
    Is there any advantage to using that over Number, other than the obviously shorter syntax?

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,862
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Pullo View Post
    That's a unary plus, right?
    Is there any advantage to using that over Number, other than the obviously shorter syntax?
    Yes it is a unary plus - the advantage of using it is that it is shorter. I don't know of any other advantage but I don't see any need to type six characters when one will do the same thing. I would not have posted that version without the version using Number also being posted as the unary plus code is basically a short version of that just as ''+ is a short version of String().
    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="^$">

  7. #7
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,003
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Oh ok, thanks.
    I didn't know the "+ notation either.


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
  •