459,399 Members | 1,319 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,399 IT Pros & Developers. It's quick & easy.

# my function needs a for loop.

 P: n/a My function works but there has got to be a way to make a for loop to handle this...but I can't get a for loop to work. You can tell, I'm not very good at this...help. "myvalue" is the number of miles the person enters "myitem" is the row...there are seven rows where they can list expenses. "myvalue" * .375 = amount to be reimbursed All the rows are added and placed into the total. function autoReimb(myvalue,myitem){ var nrow = "autoReimb" + myitem; if(isPositiveInteger(myvalue.value)){ var amount = myvalue.value * .375; formatedamt = format(amount) eval("document.forms[0]." + nrow + ".value = formatedamt"); if(isPositiveInteger(document.forms[0].miles1.value)) var reimb1 = document.forms[0].autoReimb1.value; else var reimb1 = 0; if(isPositiveInteger(document.forms[0].miles2.value)) var reimb2 = document.forms[0].autoReimb2.value; else var reimb2 = 0; if(isPositiveInteger(document.forms[0].miles3.value)) var reimb3 = document.forms[0].autoReimb3.value; else var reimb3 = 0; if(isPositiveInteger(document.forms[0].miles4.value)) var reimb4 = document.forms[0].autoReimb4.value; else var reimb4 = 0; if(isPositiveInteger(document.forms[0].miles5.value)) var reimb5 = document.forms[0].autoReimb5.value; else var reimb5 = 0; if(isPositiveInteger(document.forms[0].miles6.value)) var reimb6 = document.forms[0].autoReimb6.value; else var reimb6 = 0; if(isPositiveInteger(document.forms[0].miles5.value)) var reimb7 = document.forms[0].autoReimb7.value; else var reimb7 = 0; var totalauto = (reimb1 * 1) + (reimb2 * 1) + (reimb3 * 1) + (reimb4 * 1) + (reimb5 * 1) + (reimb6 * 1) + (reimb7 * 1); document.forms[0].autotot.value = format(totalauto); } else eval("document.forms[0]." + nrow + ".value = ''"); } //End Function Jul 23 '05 #1
8 Replies

 P: n/a "Abby Lee" wrote in message news:80**************************@posting.google.c om... My function works but there has got to be a way to make a for loop to handle this...but I can't get a for loop to work. You can tell, I'm not very good at this...help. "myvalue" is the number of miles the person enters "myitem" is the row...there are seven rows where they can list expenses. "myvalue" * .375 = amount to be reimbursed All the rows are added and placed into the total. function autoReimb(myvalue,myitem){ var nrow = "autoReimb" + myitem; if(isPositiveInteger(myvalue.value)){ var amount = myvalue.value * .375; formatedamt = format(amount) eval("document.forms[0]." + nrow + ".value = formatedamt"); if(isPositiveInteger(document.forms[0].miles1.value)) var reimb1 = document.forms[0].autoReimb1.value; else var reimb1 = 0; if(isPositiveInteger(document.forms[0].miles2.value)) var reimb2 = document.forms[0].autoReimb2.value; else var reimb2 = 0; if(isPositiveInteger(document.forms[0].miles3.value)) var reimb3 = document.forms[0].autoReimb3.value; else var reimb3 = 0; if(isPositiveInteger(document.forms[0].miles4.value)) var reimb4 = document.forms[0].autoReimb4.value; else var reimb4 = 0; if(isPositiveInteger(document.forms[0].miles5.value)) var reimb5 = document.forms[0].autoReimb5.value; else var reimb5 = 0; if(isPositiveInteger(document.forms[0].miles6.value)) var reimb6 = document.forms[0].autoReimb6.value; else var reimb6 = 0; if(isPositiveInteger(document.forms[0].miles5.value)) var reimb7 = document.forms[0].autoReimb7.value; else var reimb7 = 0; var totalauto = (reimb1 * 1) + (reimb2 * 1) + (reimb3 * 1) + (reimb4 * 1) + (reimb5 * 1) + (reimb6 * 1) + (reimb7 * 1); document.forms[0].autotot.value = format(totalauto); } else eval("document.forms[0]." + nrow + ".value = ''"); } //End Function 1) Use "document.getElementById" not "eval()". 2) You have "document.forms[0].miles5.value" twice; instead on "document.forms[0].miles7.value" 3) You should use "myvalue" not "myvalue.value". 4) It would have been easier to help you if you had included the
. 5) It can be cleaned up a lot more but the following works; watch for word-wrap. mileage.htm
=
=
=
=
=
=
=
Jul 23 '05 #2

 P: n/a On Fri, 03 Sep 2004 20:54:49 GMT, McKirahan wrote: [snip] 1) Use "document.getElementById" not "eval()". Why? Neither is the optimum solution (though gEBI is better). document.forms[0].elements['miles' + n].value gEBI requires a recent browser. The line above doesn't. [snip] 3) You should use "myvalue" not "myvalue.value". It would appear that myvalue is a form control that contains a number. If it were an actual number, undefined would result from the property access. [snip] 5) It can be cleaned up a lot more but the following works; watch for word-wrap. Yes, it could. [snip] function isPosInt(v) { return /^0|([1-9]\d*)\$/.test(v); } function autoReimb(val, itm) { var e = document.forms[0].elements, r = 'autoReimb' + itm, t = 0; if(isPosInt(val.value)) { e[r].value = format(val.value * 0.375); for(var i = 1; i <= 7; ++i) { if(isPosInt(e['miles' + i].value)) { t += +e['autoReimb' + i].value; } } e['autotot'].value = format(t); } else { e[r].value = ''; } } To the OP: Untested, but it's what I believe it's you were after. Hope that helps, Mike -- Michael Winter Replace ".invalid" with ".uk" to reply by e-mail. Jul 23 '05 #3

 P: n/a "Michael Winter" wrote in message news:opsdrxjdk4x13kvk@atlantis... On Fri, 03 Sep 2004 20:54:49 GMT, McKirahan wrote: [snip] 1) Use "document.getElementById" not "eval()". Why? Neither is the optimum solution (though gEBI is better). document.forms[0].elements['miles' + n].value gEBI requires a recent browser. The line above doesn't. http://www.jibbering.com/faq/#FAQ4_40 [snip] 3) You should use "myvalue" not "myvalue.value". It would appear that myvalue is a form control that contains a number. If it were an actual number, undefined would result from the property access. "myvalue" is a input parameter to the function: function autoReimb(myvalue,myitem){ [snip] 5) It can be cleaned up a lot more but the following works; watch for word-wrap. Yes, it could. [snip] function isPosInt(v) { return /^0|([1-9]\d*)\$/.test(v); } function autoReimb(val, itm) { var e = document.forms[0].elements, r = 'autoReimb' + itm, t = 0; if(isPosInt(val.value)) { e[r].value = format(val.value * 0.375); for(var i = 1; i <= 7; ++i) { if(isPosInt(e['miles' + i].value)) { t += +e['autoReimb' + i].value; } } e['autotot'].value = format(t); } else { e[r].value = ''; } } Good suggestion! To the OP: Untested, but it's what I believe it's you were after. Hope that helps, Mike -- Michael Winter Replace ".invalid" with ".uk" to reply by e-mail. Jul 23 '05 #4

 P: n/a On Sat, 04 Sep 2004 02:25:40 GMT, McKirahan wrote: "Michael Winter" wrote in message news:opsdrxjdk4x13kvk@atlantis... [snip] document.forms[0].elements['miles' + n].value gEBI requires a recent browser. The line above doesn't. http://www.jibbering.com/faq/#FAQ4_40 What's that meant to imply? [snip] It would appear that myvalue is a form control that contains a number. If it were an actual number, undefined would result from the property access. "myvalue" is a input parameter to the function: I know. However, the OP's original code suggests that it is a reference to a form element, not an actual value. If it were a value, myvalue.value would evaluate to undefined and I'm sure that if the OP made that mistake, it would have been explicitly mentioned ("Why does myvalue.value not give me the value I passed?") or corrected before posting to the group. If I'm wrong, then you were correct in your first post in that the OP should have shown the actual function call on the FORM element. [snip] Mike -- Michael Winter Replace ".invalid" with ".uk" to reply by e-mail. Jul 23 '05 #5