Object help needed

M

MikeS

I'm trying to add items to a shopping cart, if the item is already added I
want to just increase the quantity by 1 instead for adding a new Item, I've
got it nearly working for the first item ...but after that it just keeps
adding a new Item. What am I doing wrong - I've been racking my brains
trying to figure this out all day.

function additem(desc,price,quantity) {
if (numitems>=1){
for(i=1;i<=numitems;i++) {
if(cart.desc == desc){
++cart.quantity
}
else{
cart[++numitems] = new Item(desc,price,quantity);
}
displaycart();
}
}else{
cart[++numitems] = new Item(desc,price,quantity);
displaycart();
}
}

Thanks
Mike
 
J

Janwillem Borleffs

MikeS said:
I'm trying to add items to a shopping cart, if the item is already added I
want to just increase the quantity by 1 instead for adding a new Item, I've
got it nearly working for the first item ...but after that it just keeps
adding a new Item. What am I doing wrong - I've been racking my brains
trying to figure this out all day.

function additem(desc,price,quantity) {
if (numitems>=1){
for(i=1;i<=numitems;i++) {
if(cart.desc == desc){
++cart.quantity
}
else{
cart[++numitems] = new Item(desc,price,quantity);
}
displaycart();
}
}else{
cart[++numitems] = new Item(desc,price,quantity);
displaycart();
}
}


When the second condition equals true, the value of numitems will be
increased, which causes an infinite loop.

Instead of using [++numitems], do it like [numitems+1]. This doesn't affect
the value of numitems.


JW
 
D

Daniel

Janwillem Borleffs said:
When the second condition equals true, the value of numitems will be
increased, which causes an infinite loop.

Instead of using [++numitems], do it like [numitems+1]. This doesn't affect
the value of numitems.

Just adding to this:

The ++ operand always increases a variable's value by 1, but works in two
ways depending on where you put it -

var i = 0;
var j = 0;

alert(i++); // Outputs 0
alert(i); // Outputs 1

alert(++j); // Outputs 1
alert(j); // Outputs 1

- in other words, putting ++ *before* the variable increases its value
*before* it's evaluated, putting ++ *after* increases the value *after*. So,
these for loops do exactly the same:

for(var i=0;i<myArray.length; i++){
alert(myArray);
}

for(var i=0;i<myArray.length;){
alert(myArray[i++]);
}

for(var i=-1;i<myArray.length-1;){
alert(myArray[++i]);
}

Just in case you didn't know =)


Cheers,
Daniel
 
M

MikeS

I'm nearly there...

Ok, after looping through the array and only if the item is not found I want
to add this as a new Item What am I missing from the code below?

function additem(desc,price,quantity) {

for(i=1;i<=numitems;i++) {
if(items.desc == desc){
++items.quantity;
}
}

items[++numitems] = new Item(desc,price,quantity);

displaycart();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top