1. ## 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. 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. or a slightly shorter way of converting the strings to numbers:

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

4. Thank you very much, guys!

It works too:

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

5. Hi felgall,

Originally Posted by felgall
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. Originally Posted by Pullo
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().

7. Oh ok, thanks.
I didn't know the "+ notation either.

#### Posting Permissions

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