EDIT: There is a couple of threads suggested as an answer but none of the ideas on either of the threads quite worked. And I tried all of them.
MAJOR THANK YOU: User Eazy solved this one with his further comment in his suggestion. It turned out that the correct way to do this was to put the whole paresInt math in parenthesis:
List += "" + list[i].team + "" + (parseInt(list[i].wins) + parseInt(list[i].ties) + parseInt(list[i].lost)) ""
The original question:
So I'm making a js code to print a table from a database. The database contains a soccer league table with team names, games won, tied and lost. As I print the table to HTML I want to do the math to calculate games played and total points as the information is not provided by the database.
The strangest thing is that if I multiply (*) values from the table, it works. If I subtract (-) or divide (/) the numbers, it works. But if I try to add the numbers together with +, the code treats the numbers as strings and just writes the next to each other. I've run to this problem before and I puzzled. The solution is probably a very obvious one but I just can't figure it out.
If a team has won 23 games, tied 7 times and lost 3 times, that adds up to 33 games played. So if I do the math:
List += "<tr><td>" + list[i].team + "</td><td>" +
list[i].wins + list[i].ties + list[i].lost + "</td></tr>"
It returns 2373
If I use a different operator like multiplication:
List += "<tr><td>" + list[i].team + "</td><td>" +
list[i].wins * list[i].ties * list[i].lost + "</td></tr>"
It returns 483 wichs is correctly calculated so the items clearly are not strings. And as I said, the code works with all other operators except the +. Why is this?