<input> values are read as strings, so, like you discovered, you have to convert them to numbers first. You can use the method you came up with, or use
I'd suggest going with Lee's solution, as it is more intuitive to what you are trying to accomplish. Javascript provides us with these functions specifically for the purpose of converting strings into numbers. You can achieve the same effect by multiplying by 1, dividing by 1, or subtracting 0 (basically any math function besides + because strings see that as concatenation) - however, coding like this can become very ugly and it's not too fun to maintain - unfortunately I know this from experience (yes, I'm talking about you, Uncle Rico). When you look at your code and you see parseInt or parseFloat it is very clear what you are trying to accomplish. If you're in some competition to complete a task in the least amount of bytes possible then the tricks above will work, but from a production standpoint where others may have to help maintain the code, do everybody a favor and do it the "right" way.
Ok, off my now.
-kaht
Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.