Your values are being interpreted as strings, not numbers. See what happens when you debug:
So the comparisons are string comparisons ("alphabetically", "100" is before "20"). It's also doing string concatenation when you use the +
operator.
You need to convert the values to numbers before you use them, using Number()
, parseFloat()
, or parseInt()
, depending.