I am trying to build a simple calculator to learn javascript/jquery but seem to be getting quite confused on how to make it work. Basically at the moment I dont seem to be able to update my tally correctly, instead of the numbers/values being added together in a sum they are being concatenated instead, if anyone could give me some guidance on how I can get back on the right path with this and any other advice on how to make the code more efficient then that would be great. I don’t want anyone to give me the correct script just yet.
http://jsfiddle.net/kyllle/edRk9/4/
Thanks all in advance for your advice
The
val()method returns a string, and the+operator will concatenate if one of the operands is a string. You can convert the value to a number usingparseInt[MDN]:Note, for parsing decimal numbers, use
parseFloat[MDN], notparseInt.