473,387 Members | 1,423 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,387 software developers and data experts.

Array - how do I collect ALL items in only one list or array?

It's a very simple program, I don't know what I'm doing incorrect, all I want to do is to input items in the text box and then 'collect them in ONE list/array' and then when I press 'view' to display the product (append to existing list), My coding is shown below:

Expand|Select|Wrap|Line Numbers
  1. function letsShop() {
  2.  
  3.     var item=document.getElementById("item").value;
  4.  
  5.     var c= new Array(item);
  6.     for(var i=0; i<c.length;i++);
  7.     alert(c[i]);
  8.     alert(c.length);
  9. }
Ta
Aug 18 '07 #1
5 1762
gits
5,390 Expert Mod 4TB
hi ...

you always put the actual item in new array c ... so you always will get only ONE item to and from it ... you may use a global variable and then use the array push(item); method to extend the list everytime you call the letsShop-function ...

kind regards

ps: ok more explainations - i commented your code:

Expand|Select|Wrap|Line Numbers
  1. // function call - i assume you will add to an array here?
  2. function letsShop() {
  3.     // you declare and assign the value of an html-node here
  4.     // that's ok
  5.     var item=document.getElementById("item").value;
  6.  
  7.     // you declare the variable c here locally within the 
  8.     // function only and always assign a new array with 
  9.     // one item
  10.     var c= new Array(item);
  11.  
  12.     // this is useless ;) nothing is in the loop 
  13.     // first: you always loop through 1 element only
  14.     // second: nothing is done with the elements ??
  15.     for(var i=0; i<c.length;i++);
  16.  
  17.     // now you alert the last reached value (i) of the 
  18.     // array c -> always c[0]
  19.     alert(c[i]);
  20.  
  21.     // this is always 1
  22.     alert(c.length);
  23. }
  24.  
Aug 18 '07 #2
Thank you that, making or declaring array global did the trick.

However, when i do a sort() i'm not getting a matching list/array - 'items' are not matching 'quantity', currently 'item' array & 'quantity' array are different. Where am i going wrong? see my code:

Expand|Select|Wrap|Line Numbers
  1. var c=[];                  //item array
  2. var d=[];                 //quantity array
  3.  
  4. function letsShop() {
  5.  
  6.     var item=document.getElementById("item").value;
  7.     var quantity=document.getElementById("quantity").value;
  8.  
  9.     c[c.length]=item;
  10.     d[d.length]=quantity;
  11.  
  12.     Basket_display(item,quantity);
  13. }
  14.  
  15.  
  16. function Basket_display(item,quantity) {
  17.     document.getElementById('display').innerHTML+=item + " X "+ quantity+'<br>';    
  18. }
  19. </script> 
I think i need to 'join' the two arrays together, not sure how to do this?
Aug 18 '07 #3
gits
5,390 Expert Mod 4TB
hi ...

please use code-tags when posting code ...

to your problem ... when you need more complex data-structures, you should consider an js-object ... have a look at the following example:

Expand|Select|Wrap|Line Numbers
  1. // your list
  2. var c = [];
  3.  
  4. // within your function letsShop() declare an object
  5. // for an item
  6. function letsShop() {
  7.     // item object
  8.     var item = {};
  9.  
  10.     // assign your item name
  11.     item.name = 'foo';
  12.  
  13.     // assign your item quantity
  14.     item.quantity = 1;
  15.  
  16.     // add the item(record) to your list
  17.     c.push(item);
  18. }
  19.  
kind regards
Aug 19 '07 #4
thank you, one more final question of the day,

How & 'where' would I place the 'for loop', to print or sort etc. Would this be in another function or...

Expand|Select|Wrap|Line Numbers
  1. for(var=i, i<c.length, i++ )
Ta
Aug 19 '07 #5
gits
5,390 Expert Mod 4TB
hi ...

for the code-tag issue have a look here ...

and please make yourself a little bit more clear. you have to put the loop there where you have to iterate to do something. for example: you should put it in a seperate function when you want to call it on different places in your code so you don't need to duplicate your code -> simply call the function.

kind regards
Aug 19 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: hokieghal99 | last post by:
I wish to place all files and directories that are within a user defined path (on a Linux x86 PC) into some type of array and then examine those items for the existence of certain charaters such as...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
4
by: emma middlebrook | last post by:
Hi Straight to the point - I don't understand why System.Array derives from IList (given the methods/properties actually on IList). When designing an interface you specify a contract. Deriving...
5
by: tony collier | last post by:
This is an array question but it is based in my web page so i shall give scenario: when the client navigates to the page, the page takes some figures and does loads of calulations in page_load...
10
by: javuchi | last post by:
I just want to share some code with you, and have some comments and improvements if you want. This header file allocates and add and delete items of any kind of data from a very fast array: ...
6
by: Chris Ashley | last post by:
How can I manually release the memory held by a large array? I have tried setting the array to null but no joy. The garbage collector appears to be ignoring it completely.
6
by: Martin Arvidsson | last post by:
Hi all you gurus! Whats the biggest difference between using a string x; or an ArrayList x; When to use wich? Regards Martin
7
by: mark4asp | last post by:
How can I use name:value pairs like an array? 1. I want to load data on to the page which will be used to populate a list box (see example below): <html> <head> <script...
10
by: SM | last post by:
Hello I'm trying to create a multi dimensional array in JavaScript, but after some reading i still can't figure out how to apply it to my model. Here it is: I have a list A and for each item...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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...

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.