473,396 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Object help needed

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
3 1663

"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

"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: lawrence | last post by:
How dangerous or stupid is it for an object to have a reference to the object which contains it? If I have a class called $controllerForAll which has an arrray of all the objects that exist, what...
7
by: Eric | last post by:
My program will create a few instance of class A. Class A will always need this 256byte array that is used for bitcounting. I have a function that fills the array. How do a right a class or...
8
by: SpOiLeR | last post by:
Hello! I have a matrix class like this: class MyObject; // MyMatrix is contains MyObjects class MyMatrix { public: ...
7
by: Aaron | last post by:
Complete code follows. I am new to .NET programming (and programming in general) and I am having a difficult time understanding how to fill a variable in one sub, and then access it from...
6
by: Belebele | last post by:
Suppose I want to use some object's member function as the action passed to a for_each call: class A { ... public: void foo(int ); }; A a;
12
by: jeremito | last post by:
Please excuse me if this is obvious to others, but I can't figure it out. I am subclassing dict, but want to prevent direct changing of some key/value pairs. For this I thought I should override...
2
by: Veloz | last post by:
Hiya My question is whether or not you should associated related objects in your software using a scheme of id's and lookups, or wether objects should actually hold actual object references to...
2
by: Michael | last post by:
Hi, I try to use mysqli object instead of standard mysql functions. Is it ok to create mysqli object within my class or schould I pass mysqli object to my object. The problem is, with code...
5
by: Andy B | last post by:
I am trying to figure out how to make an object instance available for all methods of a class. I tried to do something like this: public class test { TheObject Instance = new TheObject();...
2
dream party
by: dream party | last post by:
Inserting a Flash (SWF, FLV) file into HTML web page is already an old and familiar thing to all of us. It is a rather non-flexible thing that just to edit some options in the template. However, I...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.