How do I add multiple numbers in js?
I am making a program which you add a ton of numbers to calculate the sum. So far, I got all the numbers in a variable like this:
var numbers = 123456;
How do I add all these up?
P.S: I can use a jquery solution as well!
var firstNum = 123; var secondNum = 137; console.log(firstNum + secondNum);```
You add a bunch of numbers? So are the numbers stored in different variables? Why would you store them in a single variable???
that should work, the string.split will split the numbers into 1, 2, 3, etc. and the reduce will add them together.
What I don't understand is why you don't store them in an array, storing them all in a string is such a bad idea.
- you can't determine multi-digit numbers
- have to split into array each time you use it
- probably more work