adding totals from 1 array to another

M

Mark Scott

I am trying to add some totals from one array into another and clearing down
the first array. When I run the following code, I get "undefined" rather
than the totals?

Also, the loop:

for (counter = 0; counter < productStock.length; counter = counter + 1)
{
document.write("The counter variable is: "+counter+"<BR>")
productStock[counter] = productStock[counter] + receivedLevels[counter]
receivedLevels[counter] = 0
document.write(productStock[counter]+"<BR>")
}

Doesnt appear to fire.

Regards

Mark

<SCRIPT
language="JavaScript"
type="text/javascript">
// These are arrays containing product data
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];
var receivedLevels = [50, 10, 150, 500, 50, 100];
// Write the function addAllReceivedItems() here
function addAllReceivedItems()
{
var counter // variable for counter
counter=parseFloat(counter)
productStock = parseFloat(productStock)
receivedLevels = parseFloat(receivedLevels)
for (counter = 0; counter < productStock.length; counter = counter + 1)
{
document.write("The counter variable is: "+counter+"<BR>")
productStock[counter] = productStock[counter] + receivedLevels[counter]
receivedLevels[counter] = 0
document.write(productStock[counter]+"<BR>")
}
document.write(productStock[0]+"<BR>");
document.write(productStock[1]+"<BR>");
document.write(productStock[2]+"<BR>");
document.write(productStock[3]+"<BR>");
document.write(productStock[4]+"<BR>");
document.write(productStock[5]+"<BR>");
}
addAllReceivedItems()
</SCRIPT>
 
S

SAM

Mark Scott a écrit :
<SCRIPT
language="JavaScript"
type="text/javascript">
// These are arrays containing product data
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];
var receivedLevels = [50, 10, 150, 500, 50, 100];
// Write the function addAllReceivedItems() here
function addAllReceivedItems()
{
var counter // variable for counter
counter=parseFloat(counter)

do you know what you're doing when you code that line bellow ?
productStock = parseFloat(productStock)

You can parseFloat a string but certainly not an array

<http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Functions:parseFloat>
<http://www.w3schools.com/jsref/jsref_parseFloat.asp>

suppress these two parseFloat ans all will be OK
receivedLevels = parseFloat(receivedLevels)
for (counter = 0; counter < productStock.length; counter = counter + 1)
{
document.write("The counter variable is: "+counter+"<BR>")
productStock[counter] = productStock[counter] + receivedLevels[counter]
receivedLevels[counter] = 0
document.write(productStock[counter]+"<BR>")
}
document.write(productStock[0]+"<BR>");
document.write(productStock[1]+"<BR>");
document.write(productStock[2]+"<BR>");
document.write(productStock[3]+"<BR>");
document.write(productStock[4]+"<BR>");
document.write(productStock[5]+"<BR>");
}
addAllReceivedItems()
</SCRIPT>
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top