0

I have an array with 25 slots and each slot fills up with the click of the corresponding button. I'm having an issue summing up the values of the array. I have tried

for(var k = 0; k < price.length; k++)
{
    totalPrice += price[k] << 0;
}

But this just seems to append the price onto the end of the previous output instead of adding together.

My current code is this:

$("#sumArray").click(function()
{       
    a++;
    price[0] = 8.45 * a;
    alert(price[0]);
    for(var k = 0; k < price.length; k++)
    {
        totalPrice += price[k] << 0;
    }
    alert(totalPrice);
    for(var i = 0; i < 27; i++)
    {
        $("#priceHere"+i+"").append("<b>"+totalPrice+"</b>");
    }
    //$.mobile.changePage("#mainMenu1");
});

All the other array indexes get filled in other click functions.

EDIT: Removed all code not related to question for ability to read easier. the price array indexes can change depending on how many times a certain button is clicked.

8
  • I assume your referring to the text array and commented out totalPrice variable? Commented Jul 19, 2015 at 5:27
  • You might consider converting those text values to something which can be numerically added? otherwise you'll only get concatenation. Commented Jul 19, 2015 at 5:30
  • @QuinnRoundy the text array isn't my issue atm. It's the price array Commented Jul 19, 2015 at 5:40
  • Make a jsfiddle, and specify assumptions and requirements at one place (such as you are doing for mobile, and array values change) not here and there in comments Commented Jul 19, 2015 at 5:40
  • @cswl Yeah, that's just there temporarily. I'm gonna make it cleaner later on Commented Jul 19, 2015 at 5:41

4 Answers 4

2

For modern browsesrs (IE 9+ and anything else)

total = price.reduce(function (prev,curr) {
   return prev + curr;
}, 0);

ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

Sign up to request clarification or add additional context in comments.

2 Comments

I should note that I am doing this for a mobile app, so browser compatibility isn't a thing I need to worry about
Perfect. .reduce() is made exactly for things like summing an array.
1

Add this before your calculation:

totalPrice = 0;

If you don't reset the value it just keeps adding to the previous output.

Since the question keeps being edited:

$("#priceHere"+i+"").append("<b>"+totalPrice+"</b>");

Does, what it says. It appends the total price to the element. If you want to overwrite the value completely, you could do:

$("#priceHere"+i+"").html("<b>"+totalPrice+"</b>");

5 Comments

Yea, "lol". I did read the question. Though I might update my answer since the OP edited his question several times.
Sorry, that came out wrong.. I meant as '+1 read(raed) the question throughly.. ' as in 'read as'.... but SO didnt let me type +1.. and english has same verb read, read read,,.
Oh then I misunderstood that, haha. No worries, it's all fine.
@cswl Just looked at your code. Not sure where you need help, since it seems to work as expected. But maybe shouldn't discuss that in the comments here lol.
0

Where do you declare price and totalPrice?

Here's an example of summing the values of an array:

var price = [1, 2, 3, 4, 5], // example array of prices
    totalPrice = 0, // initialize to 0 so totalPrice is a number
    k; // counter that you'll use in the loop

for (k = 0; k < price.length; k++) {
  totalPrice += price[k];
}

console.log(totalPrice); // 15

Note that JavaScript uses the + operator for both addition and string concatenation. It sounds like what is likely happening is that totalPrice has been set as a string (or type coerced into one), and thus when you're meaning to add to a number, you're in fact concatenating a string.

Here's an example of what that looks like. Note that all of the code is identical, save for the assignment of a string (rather than a number) to totalPrice:

var price = [1, 2, 3, 4, 5], // example array of prices
    totalPrice = '0', // initialized as the string '0'
    k; // counter that you'll use in the loop

for (k = 0; k < price.length; k++) {
  totalPrice += price[k];
}

console.log(totalPrice); // '012345'

1 Comment

They are declared globally
0

As per your question, you need nothing more than this

price = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5]
totalPrice = 0
for(var k = 0; k < price.length; k++) {totalPrice += price[k];}
// gives 105

But this just seems to append the price onto the end of the previous output instead of adding together.

Make it zero:

totalPrice = 0

4 Comments

The array values aren't preset, they change each time the button is clicked. I have tried this way as well, it just seems to take the values from the first click and and the value from the second click (even though they are in the same index) and add them together. They also just append to one another in priceHere
Specify this properly in question that array values can change
I would think looking at price[0] = 8.45 * a; you could see that it has a possibility to change
If you clear that up in question, people can think more on solution than understanding your question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.