1

I have an array, the value of the first element is 1.

Every time a user clicks a button, I want it to increase by 1. I have this written, but it's making the array element go from 1, to 11, to 111... and so on. How do I just get it to increase by 1. Like 1, 2, 3...

function addCake (){
 quantityArray[0] += 1;
 var cost= quantityArray[0] * 1.49;
 document.getElementById("cart").innerHTML += "<tr id="+"cakeTotal"+"><td>"+quantityArray[0]+"</td>Cake Donut</td><td>"+"$"+cost+"</td>";
}
Eswara Reddy
1,6471 gold badge18 silver badges28 bronze badges
asked Aug 19, 2013 at 5:28
1
  • How and when are you initializing this array? Commented Aug 19, 2013 at 5:30

8 Answers 8

3

Change your code as below :

function addCake (){
 quantityArray[0]=parseInt(quantityArray[0]) + 1;
 var cost= parseInt(quantityArray[0]) * 1.49;
 document.getElementById("cart").innerHTML += "<tr id="+"cakeTotal"+"><td>"+quantityArray[0]+"</td>Cake Donut</td><td>"+"$"+cost+"</td>";
}
answered Aug 19, 2013 at 5:30
Sign up to request clarification or add additional context in comments.

Comments

2

Try to convert it to int

quantityArray[0] = parseInt(quantityArray[0]);
quantityArray[0] += 1;

The problem I see is that you are initalizating the array items as "".

Why? if you set as 0, you can avoid the problem: http://jsfiddle.net/rgvaT/2/

quantityArray[0]=0;
answered Aug 19, 2013 at 5:28

4 Comments

i think you need to edit it to parseInt(quantityArray[0]) because 1 is int by default.
this would probably work, but it starts at 0, so it's giving me NaN
@Ktown Ah! can you post some more code? like on a fiddle? It would be easier to write the precise code you need that way
@Ktown Edited the answer with your fiddle
2

You could try:

quantityArray[0]++;

This will also add 1 to your counter.

answered Aug 19, 2013 at 5:31

Comments

2

No need to call parseInt etc. Just initialize your quantityArray array properly:

var quantityArray = [];
quantityArray[0] = 0;
function addCake (){
 quantityArray[0]++;
 var cost = quantityArray[0] * 1.49;
 document.getElementById("cart").innerHTML += "<tr id="+"cakeTotal"+"><td>"+quantityArray[0]+"</td>Cake Donut</td><td>"+"$"+cost+"</td>";
}
answered Aug 19, 2013 at 5:39

1 Comment

@ktown: Dont initialize your array element like this: quantityArray[0]="";. As I suggested above make it quantityArray[0]=0;
1

Try this

quantityArray[0]=parseInt(quantityArray[0]) + 1;
answered Aug 19, 2013 at 5:32

1 Comment

Does 1 require parseInt?
0
var test = new Array();
 test[0] = 1;
 test[0] += 1;
 test[0] += 1;
alert(test[0]); // output is 3. Its working fine.

What you are doing is concatenating the "String" 1 with previous value of array.

To avoid that cast the value to int, using parseInt(). Something like this.

function addCake (){
 quantityArray[0] += parseInt(1, 10); // To decimal. It is very important
 var cost= quantityArray[0] * 1.49;
 document.getElementById("cart").innerHTML += "<tr id="+"cakeTotal"+"><td>"+quantityArray[0]+"</td>Cake Donut</td><td>"+"$"+cost+"</td>";
}
answered Aug 19, 2013 at 5:36

Comments

0

I guess you have initialized the array element as string. use below code to increment the element .

quantityArray[0] = parseInt(quantityArray[0]) + 1;
answered Aug 19, 2013 at 5:37

Comments

0

Here The Problem is with They datatype

In JavaScript, when you declare a variable by var x; It stores the variable as an object of type var.That is what causing the problem.

You need to Convert the object ( var ) to an integer to do mathematical operations by type casting. You can typecast using (Number) function. In other cases it consider + as an append operator and variables as String do append function.

The code will looks like this.

function addCake (){
 quantityArray[0] =(Number)quantityArray[0]+1;
 var cost= quantityArray[0] * 1.49;
 document.getElementById("cart").innerHTML += "<tr id="+"cakeTotal"+"><td>"+quantityArray[0]+"</td>Cake Donut</td><td>"+"$"+cost+"</td>";
}
answered Aug 19, 2013 at 5:56

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.