By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,381 Members | 1,626 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,381 IT Pros & Developers. It's quick & easy.

Object help needed

P: n/a
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[i].desc == desc){
++cart[i].quantity
}
else{
cart[++numitems] = new Item(desc,price,quantity);
}
displaycart();
}
}else{
cart[++numitems] = new Item(desc,price,quantity);
displaycart();
}
}

Thanks
Mike
Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"MikeS" <no****@secure.f9.co.uk> schreef in bericht
news:be************@ID-27598.news.dfncis.de...
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[i].desc == desc){
++cart[i].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

Jul 20 '05 #2

P: n/a

"Janwillem Borleffs" <jw*@jwbfoto.demon.nl> wrote in message
news:3f***********************@news.euronet.nl...
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[i]);
}

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

--
There are 10 kinds of people: Those who know binary and those who don't.
Jul 20 '05 #3

P: n/a
"MikeS" <no****@secure.f9.co.uk> wrote in message
news:be************@ID-27598.news.dfncis.de...

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[i].desc == desc){
++items[i].quantity;
}
}

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

displaycart();
}

Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.