WackyMole
WackyMole

Reputation: 49

javascript adding variables on top

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

Upvotes: 0

Views: 44

Answers (1)

Matt Zeunert
Matt Zeunert

Reputation: 16561

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.

Upvotes: 3

Related Questions