Jump to content

Using arrays in javascript


Rubiconlwb

Recommended Posts

I have a website where I defined a number of items with variables that represent name, price, items on hand, photo taken, etc. All has worked well until I reached the magical number of 42 items. When I define that 42nd item I get undefined instead of the correct info. Do I need to define the number of items that will be in the array before creating them? Below is the routine that I use to show the photos with the appropriate info shown.

 

function PrintLeft(url,item,price){ document.write("<tr style='text-align:center;'><td>"+url+item+" ($"+price+".00)</td>");}function PrintRight(url,item,price){ if(url===""){ document.write("<td>New Item Coming Soon!</td></tr>"); } else { document.write("<td>"+url+item+" ($"+price+".00)</td></tr>"); }}

 

var left=41;PrintLeft("<img src='/pictures/Toothpick holder.jpg' width='400' height='266' alt='"+shortname

+"' title='"+shortname

+"'/>",project

,price

);var right=42;PrintRight("<img src='/pictures/Round wood box.jpg' width='400' height='266' alt='"+shortname

+"' title='"+shortname

+"' />",project

,price

);

 

I defined the arrays like this:

//itemcounter=42project[itemcounter] = "Round wooden box with lid";price[itemcounter]=6;onhand[itemcounter]=2;photo[itemcountedr]=1;shortname[itemcounter]="roundbox";itemcounter++

 

I'm pretty new to javascript arrays and completely self taught so my techniques may be a little rough but any suggestions appreciated

 

Link to comment
Share on other sites

Watch your typing, this typo would cause the element to not exist at the specified index:

photo[itemcountedr]=1;

In Javascript you don't need to define the length of the array before using it, but watch out when setting the index of an array. If you create a new array and only set index 5, elements 0, 1, 2, 3 and 4 will exist but with an undefined value.

 

Without seeing your actual code I can't tell where the problem is. It has nothing to do with what number you use for the index, it's most likely that due to a mistake, you didn't create element 42.

Link to comment
Share on other sites

Thank you so much Ingolme, what you pointed out was exactly what the problem was. I thought I had done every bit of troubleshooting possible to isolate the problem but obviously I missed the misspelling. I also had the same problem on the element before the last one (I copied this element to create the new element). Once I corrected those errors, it worked fine. I can't tell you what a load that is off of my mind. I probably could have looked at the code for another week before picking out the improper spelling. Once again, thanks a ton! I'm now back on track.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...