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>";
}
-
How and when are you initializing this array?Itay– Itay2013年08月19日 05:30:13 +00:00Commented Aug 19, 2013 at 5:30
8 Answers 8
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>";
}
Comments
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;
4 Comments
parseInt(quantityArray[0]) because 1 is int by default.You could try:
quantityArray[0]++;
This will also add 1 to your counter.
Comments
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>";
}
1 Comment
quantityArray[0]="";. As I suggested above make it quantityArray[0]=0;Try this
quantityArray[0]=parseInt(quantityArray[0]) + 1;
1 Comment
1 require parseInt?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>";
}
Comments
I guess you have initialized the array element as string. use below code to increment the element .
quantityArray[0] = parseInt(quantityArray[0]) + 1;
Comments
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>";
}