I can not seem to get this to work. It is the array: function this(){ var a = [3,2,2,3,4,1]; var b = [1,2,2,1,1,2]; var c = [34,21,20,12,30,25]; for (k = 0; k <= 5; k++){ var sumR1 = a[k] * b[k] + c[k] ; var sumR = sumR + sumR1; }//Next k var Req = (385000.56 + sumR / 10); window.document.myform.sum1.value = Req; } Code (markup):
array initialization in javascript not like that. try this function this(){ var a = new Array(3,2,2,3,4,1); var b = new Array(1,2,2,1,1,2); var c = new Array(34,21,20,12,30,25); for (k = 0; k <= 5; k++){ var sumR1 = a[k] * b[k] + c[k] ; var sumR = sumR + sumR1; }//Next k var Req = (385000.56 + sumR / 10); window.document.myform.sum1.value = Req; } Code (markup):
Two main problems: 1. the accumulator variable 'sumR' is being re-declared within each loop, so it can never accumulate across loops. 2. 'this' is a Reserved Word in JavaScript, and should not be used as a function name or a variable. It also seems the variables sumR1 and Req are not necessary there. See how the following works. I changed the name of the function to doThis(); don't forget to modify the function call accordingly. BTW, there's nothing at all wrong with declaring JavaScript arrays with square brackets. Many prefer to do it that way. function doThis(){ var a = [3,2,2,3,4,1]; var b = [1,2,2,1,1,2]; var c = [34,21,20,12,30,25]; var sumR = 0; for (var k = 0; k <= 5; k++){ sumR += a[k] * b[k] + c[k] ; }//Next k document.myform.sum1.value = (385000.56 + sumR / 10); } Code (markup):
nicely done. I can't seem to get the question but now that i have read your answer i am enlighten. thanks!
Stay away from `Array` constructor syntax. Especially for novices, it may lead to confusion between: new Array(element0, element1, ..., elementN) new Array(arrayLength) Code (markup):
can you try this cod plz and tell me function this(){ var a = new Array(3,2,2,3,4,1); var b = new Array(1,2,2,1,1,2); var c = new Array(34,21,20,12,30,25); for (k = 0; k
1) You're not declaring sumR before the loop, so christmas knows what it's initial value is. 2) Avoid declaring variables you don't actually need. 3) try using prefixed additions, it's less code. 4) accessing form elements by name like that is nyetscape 4 style coding... since you're stuffing it into a form element, it should have a perfectly good ID on it -- if for no other reason than something it's LABEL should be pointing at. function this() { var a=[ 3, 2, 2, 3, 4, 1]; var b=[ 1, 2, 2, 1, 1, 2]; var c=[34,21,20,12,30,25]; var sumR=0; for (k=0; k<6; k++) sumR+=a[k]*b[k]+c[k]; document.getElementById('sum1').value=(385000.56+sumR/10); } Code (markup): Where 'sum1' is the target form element's ID. I'd also consider making that a single array, so you can auto-detect length instead of hardcoding it. function this() { var data=[ [3,1,34], [2,2,21], [2,2,20], [3,1,12], [4,1,30], [1,2,25] ]; var sum=0; for (k=0; k<data.length; k++) sum+=data[k][0]*data[k][1]+data[k][2]; document.getElementById('sum1').value=(385000.56+sum/10); } Code (markup):