D&C GLug - Home Page

[ Date Index ] [ Thread Index ] [ <= Previous by date / thread ] [ Next by date / thread => ]

Re: [LUG] Some help with javascript & firefox

 

Kevin Lucas wrote:
>
>
> Steven Cote wrote:
>>
>>     have I done it wrong?
>>
>>
>> Yep, there's a couple mistakes in your version. I've actually taken 
>> the time to test my code this time, so this should definitely work. :)
>>
>> You're reinitialising the value of sum in the for loop, so no actual 
>> addition will occur. Also, you're using a straight assignment, so the 
>> value from previous iterations is just getting overwritten. The 
>> "missing ; before statement error" was because you missed the single 
>> quotes around the word docuement and value, so it was trying to 
>> actually evaluate document.splitfm.vat_low (which doesn't actually 
>> exist). The last bit that we both missed was converting the evaluated 
>> sum from a string to an int. Oh yeah, and you missed the < sign in 
>> the for control, so you had an infinite loop. So, put all that 
>> together and you get a code block that looks like:
>>
>> var sum = 0;
>> for( var i = 0; i <= rowNum; i++ ) {
>>    var vat_low = 'sum += parseint(document.splitfm.vat_low' + i + 
>> '.value)';
>>    eval(vat_low);
>> }
>>
>>
> now I get
> parseint is not defined
> http://www.minionsbandb.co.uk/oldsplitaddtable.html
> Line 106
>
> what does the += mean after sum?
+= is the Javascript shorthand for
a = a + b
rewritten as

a += b

In other words 'add b to a'.

HTH

Personally I wouldn't keep redefining vat_low all the time in the loop.

var sum = 0;
var vat_low[rowNum] = 0;  // assign vat_low as an array of numbers

for (i = 0; i<=rowNum; i++)
{
    vat_low[i]  = sum += parseint(document.splitfm.vat_low.value);
    eval(vat_low[i])'
}

Admittedly I have only done Javascript under sufferance in university, 
but I think the above makes sense.

Kind regards,

Julian

-- 
The Mailing List for the Devon & Cornwall LUG
http://mailman.dclug.org.uk/listinfo/list
FAQ: http://www.dcglug.org.uk/linux_adm/list-faq.html