help with 'for' loop, this is a weird problem

Discussion in 'JavaScript' started by syntrax, Oct 4, 2010.

  1. #1
    Ok, so, i have this code, that's suppose to be running using 'for' loop only.
    i know the code works, b/c when i do line by line manually, it works, but not when i do it in the loop, here's the code:

    
    var asdf 	= new Array('lb5a', 'lb10a', 'lb20a', 'lb30a', 'lb33a', 'lb40a', 'lb43a', 'lb50a', 'lb60a', 'lb100a', 'rva', 'motorFuela');
    	var cyl		= new Array('lb5', 'lb10', 'lb20', 'lb30', 'lb33', 'lb40', 'lb43', 'lb50', 'lb60', 'lb100', 'rv', 'motorFuel');
    	var prices	= new Array(11, 13, 15, 25, 22, 30, 32, 35, 45, 68, 2.75, 3.00);
    	var formVar	= new Array('cyl5', 'cyl10', 'cyl20', 'cyl30', 'cyl33', 'cyl40', 'cyl43', 'cyl50', 'cyl60', 'cyl100', 'rvs', 'motorFuel');
    
    for(var i=0; i<asdf.length; i++)
    	{
    		// this is where i ALWAYS get the error, it says that this has not been
            // been initialized,,, help plz! this is driving me nuts
    		asdf[i] = (document.frm.cyl[i].value * prices[i]);
    		
    		// display the prices
    		document.frm.formVar[i].value = asdf[i];
    	}
    
    Code (markup):

     
    syntrax, Oct 4, 2010 IP
  2. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #2
    Try this code, I think it will work

    
    var asdf = new Array('lb5a', 'lb10a', 'lb20a', 'lb30a', 'lb33a', 'lb40a', 'lb43a', 'lb50a', 'lb60a', 'lb100a', 'rva', 'motorFuela');
    var cyl	= new Array('lb5', 'lb10', 'lb20', 'lb30', 'lb33', 'lb40', 'lb43', 'lb50', 'lb60', 'lb100', 'rv', 'motorFuel');
    var prices	= new Array(11, 13, 15, 25, 22, 30, 32, 35, 45, 68, 2.75, 3.00);
    var formVar	= new Array('cyl5', 'cyl10', 'cyl20', 'cyl30', 'cyl33', 'cyl40', 'cyl43', 'cyl50', 'cyl60', 'cyl100', 'rvs', 'motorFuel');
    
    for(var i=0; i<asdf.length; i++)
    	{
    		// this is where i ALWAYS get the error, it says that this has not been
            // been initialized,,, help plz! this is driving me nuts
    		asdf[i] = (document.frm[cyl[i]].value * prices[i]);
    
    		// display the prices
    		document.frm[formVar[i]].value = asdf[i];
    	}
    
    Code (JavaScript):
     
    s_ruben, Oct 5, 2010 IP
  3. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #3
    asdf = (document.frm.cyl.value * prices); << why are you using document.frm? try:
    asdf = (parseInt(cyl) * prices);

    I'm unsure of exactly what you are trying to do here. Can you provide the HTML code aswell.
     
    camjohnson95, Oct 6, 2010 IP