Looping through Arrays - variable undefined?

M

Mark Scott

Hi

If I choose to buy one product the output is what I expect, ie the product
is listed. If I choose to buy more then one product, the output shows that
code and product are undefined? I am sure that the error is in the
section:

for (orderIndex = 0; orderIndex < purchaseCount; orderIndex = orderIndex +
1)
{
document.write
(productCodes[orderProductArray]+"-------"+productDescriptions[orderProductArray]+"--------"+orderQuantityArray[orderIndex]

+"<BR>")
}

but I am having a time trying to solve. any ideas?

Cheers

Mark

// These are arrays containing product data
var productCodes = ['a-23-009', 'k-246-07', 'd-555-01', 'n-87-012',
'u-521-08', 't-99-002'];
var productDescriptions = ['Superslurry electric blender', 'Apple - iPod
(second-hand)', 'CoziNap nylon duvet tog 2', 'Headbanger mini

hi-fi 20W', 'MagiBoot shoe cleaning kit', 'The PushmiPulu lawnmower'];
var productPrices = [45, 50, 15, 25, 75, 70];
var productStock = [200, 0, 2, 500, 500, 15];
// These are useful variables
var productIndex
document.write("Welcome, Customer. Ready to start shopping?<BR>")
document.write("<BR>Here are the exciting products we have available
today:<BR>")
document.write ("=========================================<BR>")
document.write("Item---Code--------Product-------------------------Price---Stock<BR>") for (productIndex = 0; productIndex < productCodes.length; productIndex =productIndex + 1){ document.write(productIndex+"---"+productCodes[productIndex]+"------"+productDescriptions[productIndex]+"-----"+productPrices[productIndex]+"---"+productStock[productIndex]+"<BR>")}var purchase, purchaseCount, orderIndexpurchaseCount = window.prompt ("Please enter the number of differentproducts you wish to purchase today:","")var orderProductArray = new Array(purchaseCount)var orderQuantityArray = new Array(purchaseCount)for (var purchase = 0; purchase < purchaseCount; purchase = purchase + 1){ orderProductArray[purchase] = window.prompt("Please Enter the ProductCode","") orderQuantityArray[purchase] = window.prompt("Please Enter theQuantity","")}document.write ("<BR>Here is your order <BR>")document.write("Code--------Product------------------------Number----------PriceEach<BR>")document.write ("=========================================<BR>")for (orderIndex = 0; orderIndex < purchaseCount; orderIndex = orderIndex +1){document.write(productCodes[orderProductArray]+"-------"+productDescriptions[orderProductArray]+"--------"+orderQuantityArray[orderIndex]+"<BR>")}
 
D

Doug Gunnoe

Hi

If I choose to buy one product the output is what I expect, ie the product
is listed.   If I choose to buy more then one product, the output shows that
code and product are undefined?   I am sure that the error is in the
section:

for (orderIndex = 0; orderIndex < purchaseCount; orderIndex = orderIndex +
1)
{
document.write
(productCodes[orderProductArray]+"-------"+productDescriptions[orderProduct­Array]+"--------"+orderQuantityArray[orderIndex]

+"<BR>")

}

but I am having a time trying to solve.   any ideas?

Cheers

Mark

// These are arrays containing product data
var productCodes = ['a-23-009', 'k-246-07', 'd-555-01', 'n-87-012',
'u-521-08', 't-99-002'];
var productDescriptions = ['Superslurry electric blender', 'Apple - iPod
(second-hand)',  'CoziNap nylon duvet tog 2', 'Headbanger mini

hi-fi 20W', 'MagiBoot shoe cleaning kit', 'The PushmiPulu lawnmower'];
var productPrices = [45, 50, 15, 25, 75, 70];
var productStock = [200, 0, 2, 500, 500, 15];
// These are useful variables
var productIndex
 document.write("Welcome, Customer. Ready to start shopping?<BR>")
 document.write("<BR>Here are the exciting products we have available
today:<BR>")
 document.write ("=========================================<BR>")
 document.write("Item---Code--------Product-------------------------Price---­Stock<BR>") for (productIndex = 0; productIndex < productCodes.length; productIndex =productIndex + 1){ document.write(productIndex+"---"+productCodes[productIndex]+"------"+produ­ctDescriptions[productIndex]+"-----"+productPrices[productIndex]+"---"+prod­uctStock[productIndex]+"<BR>")}var purchase, purchaseCount, orderIndexpurchaseCount = window.prompt ("Please enter the number of differentproducts you wish to purchase today:","")var orderProductArray = new Array(purchaseCount)var orderQuantityArray = newArray(purchaseCount)for (var purchase = 0; purchase < purchaseCount; purchase = purchase + 1){ orderProductArray[purchase] = window.prompt("Please Enter the ProductCode","") orderQuantityArray[purchase] = window.prompt("Please Enter theQuantity","")}document.write ("<BR>Here is your order <BR>")document.write("Code--------Product------------------------Number----­------PriceEach<BR>")document.write ("=========================================<BR>")for (orderIndex = 0; orderIndex < purchaseCount; orderIndex= orderIndex +1){document.write(productCodes[orderProductArray]+"-------"+productDescrip­tions[orderProductArray]+"--------"+orderQuantityArray[orderIndex]+"<BR>")}

From this: productCodes[orderProductArray]

productCodes is an array and orderProductArray is an array. So are you
trying to index an array with an array?
 
M

Mark Scott

Yes as in Parallel arrays?

Hi

If I choose to buy one product the output is what I expect, ie the product
is listed. If I choose to buy more then one product, the output shows that
code and product are undefined? I am sure that the error is in the
section:

for (orderIndex = 0; orderIndex < purchaseCount; orderIndex = orderIndex +
1)
{
document.write
(productCodes[orderProductArray]+"-------"+productDescriptions[orderProduct­Array]+"--------"+orderQuantityArray[orderIndex]

+"<BR>")

}

but I am having a time trying to solve. any ideas?

Cheers

Mark

// These are arrays containing product data
var productCodes = ['a-23-009', 'k-246-07', 'd-555-01', 'n-87-012',
'u-521-08', 't-99-002'];
var productDescriptions = ['Superslurry electric blender', 'Apple - iPod
(second-hand)', 'CoziNap nylon duvet tog 2', 'Headbanger mini

hi-fi 20W', 'MagiBoot shoe cleaning kit', 'The PushmiPulu lawnmower'];
var productPrices = [45, 50, 15, 25, 75, 70];
var productStock = [200, 0, 2, 500, 500, 15];
// These are useful variables
var productIndex
document.write("Welcome, Customer. Ready to start shopping?<BR>")
document.write("<BR>Here are the exciting products we have available
today:<BR>")
document.write ("=========================================<BR>")
document.write("Item---Code--------Product-------------------------Price---­Stock<BR>")
for (productIndex = 0; productIndex < productCodes.length; productIndex
=productIndex + 1){
document.write(productIndex+"---"+productCodes[productIndex]+"------"+produ­ctDescriptions[productIndex]+"-----"+productPrices[productIndex]+"---"+prod­uctStock[productIndex]+"<BR>")}var
purchase, purchaseCount, orderIndexpurchaseCount = window.prompt ("Please
enter the number of differentproducts you wish to purchase today:","")var
orderProductArray = new Array(purchaseCount)var orderQuantityArray = new
Array(purchaseCount)for (var purchase = 0; purchase < purchaseCount;
purchase = purchase + 1){ orderProductArray[purchase] =
window.prompt("Please Enter the ProductCode","")
orderQuantityArray[purchase] = window.prompt("Please Enter
theQuantity","")}document.write ("<BR>Here is your order
<BR>")document.write("Code--------Product------------------------Number----­------PriceEach<BR>")document.write
("=========================================<BR>")for (orderIndex = 0;
orderIndex < purchaseCount; orderIndex = orderIndex
+1){document.write(productCodes[orderProductArray]+"-------"+productDescrip­tions[orderProductArray]+"--------"+orderQuantityArray[orderIndex]+"<BR>")}

From this: productCodes[orderProductArray]

productCodes is an array and orderProductArray is an array. So are you
trying to index an array with an array?
 
V

VK

....
var orderProductArray = new Array(purchaseCount);
....
productCodes[orderProductArray]

As it was already pointed out, this code doesn't have a programmatic
sense, at least not in Javascript. You cannot index array elements
using other array object as an index value. So whatever you are doing
will never work as planned. I'm guessing that you are trying to
emulate a multi-dimensional array but your data structure is not clear
to me to suggest some practical code. If you would describe the
structure (involved arrays, input data, expected output) then I would
be glad to help.
 
T

Thomas 'PointedEars' Lahn

VK said:
...
var orderProductArray = new Array(purchaseCount);
...
productCodes[orderProductArray]

As it was already pointed out, this code doesn't have a programmatic
sense, at least not in Javascript.

Yes, it has, especially in ECMAScript implementations like JavaScript.
You cannot index array elements using other array object as an index value.

The argument of the bracket property accessor is implicitly type-converted
to string, and the default string representation of an Array object is a
comma-separated list of the string representation of each of its elements,
as per Array.prototype.toString(). The return value of that method
constitutes a valid property name that might be used as such.


PointedEars
 
D

Doug Gunnoe

The argument of the bracket property accessor is implicitly type-converted
to string, and the default string representation of an Array object is a
comma-separated list of the string representation of each of its elements,
as per Array.prototype.toString().  The return value of that method
constitutes a valid property name that might be used as such.

But in this case I think the OP just made a mistake.
 
T

Thomas 'PointedEars' Lahn

Doug said:
But in this case I think the OP just made a mistake.

That is a possiblity. Unfortunately, it is unlikely that someone
knowledgeable is going to analyze the OP's code due to its increasing
their chance of getting eye cancer.


PointedEars
 

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,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top