0

Okay this should be a simple one

            var width1 = size3 + 275 ; // adding more
            var width2 = 225 - size1; // subtracting width

width2 is coming out fine, for example if size1 = 4 width2 = 221. However if size3 is 4 the width1 comes out 4275. It keeps adding it to the front or back. I don't know why. ( I have flipped the size3 + 275, back and forth {275+size3} even put '275' in quotes.

Thanks for your help

2
  • 2
    common problem. You're adding to a string. Use +size3 + 275 Commented Apr 10, 2013 at 22:54
  • Or you could multiply it by 1, var width1 = size3 * 1 + 275. I don't know which method has least overhead... Commented Apr 10, 2013 at 23:01

1 Answer 1

3

The size value is treated as a string because it's in quotes, use parseInt to turn it into an integer:

var width1 = parseInt(size3, 10) + 275 ; // adding more
var width2 = 225 - parseInt(size1, 10); // subtracting width

If size is a string the + operator is used to concatenate rather than add.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks you very much. Was thinking something like that. Although I did not know parseInt could fix it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.