473,794 Members | 2,754 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

my function needs a for loop.

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(myval ue,myitem){
var nrow = "autoReimb" + myitem;
if(isPositiveIn teger(myvalue.v alue)){
var amount = myvalue.value * .375;
formatedamt = format(amount)
eval("document. forms[0]." + nrow + ".value = formatedamt");
if(isPositiveIn teger(document. forms[0].miles1.value))
var reimb1 = document.forms[0].autoReimb1.val ue;
else var reimb1 = 0;
if(isPositiveIn teger(document. forms[0].miles2.value))
var reimb2 = document.forms[0].autoReimb2.val ue;
else var reimb2 = 0;
if(isPositiveIn teger(document. forms[0].miles3.value))
var reimb3 = document.forms[0].autoReimb3.val ue;
else var reimb3 = 0;
if(isPositiveIn teger(document. forms[0].miles4.value))
var reimb4 = document.forms[0].autoReimb4.val ue;
else var reimb4 = 0;
if(isPositiveIn teger(document. forms[0].miles5.value))
var reimb5 = document.forms[0].autoReimb5.val ue;
else var reimb5 = 0;
if(isPositiveIn teger(document. forms[0].miles6.value))
var reimb6 = document.forms[0].autoReimb6.val ue;
else var reimb6 = 0;
if(isPositiveIn teger(document. forms[0].miles5.value))
var reimb7 = document.forms[0].autoReimb7.val ue;
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(totalaut o);
}
else eval("document. forms[0]." + nrow + ".value = ''");
} //End Function
Jul 23 '05 #1
8 1692
"Abby Lee" <ab*******@hotm ail.com> wrote in message
news:80******** *************** ***@posting.goo gle.com...
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(myval ue,myitem){
var nrow = "autoReimb" + myitem;
if(isPositiveIn teger(myvalue.v alue)){
var amount = myvalue.value * .375;
formatedamt = format(amount)
eval("document. forms[0]." + nrow + ".value = formatedamt");
if(isPositiveIn teger(document. forms[0].miles1.value))
var reimb1 = document.forms[0].autoReimb1.val ue;
else var reimb1 = 0;
if(isPositiveIn teger(document. forms[0].miles2.value))
var reimb2 = document.forms[0].autoReimb2.val ue;
else var reimb2 = 0;
if(isPositiveIn teger(document. forms[0].miles3.value))
var reimb3 = document.forms[0].autoReimb3.val ue;
else var reimb3 = 0;
if(isPositiveIn teger(document. forms[0].miles4.value))
var reimb4 = document.forms[0].autoReimb4.val ue;
else var reimb4 = 0;
if(isPositiveIn teger(document. forms[0].miles5.value))
var reimb5 = document.forms[0].autoReimb5.val ue;
else var reimb5 = 0;
if(isPositiveIn teger(document. forms[0].miles6.value))
var reimb6 = document.forms[0].autoReimb6.val ue;
else var reimb6 = 0;
if(isPositiveIn teger(document. forms[0].miles5.value))
var reimb7 = document.forms[0].autoReimb7.val ue;
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(totalaut o);
}
else eval("document. forms[0]." + nrow + ".value = ''");
} //End Function

1) Use "document.getEl ementById" not "eval()".

2) You have "document.f orms[0].miles5.value" twice;
instead on "document.f orms[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 <form>.

5) It can be cleaned up a lot more but the following works; watch for
word-wrap.

<html>
<head>
<title>mileage. htm</title>
<script type="text/javascript">
/*
* 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 validate() {
// autoReimb(20,2)
for (var i=1; i<8; i++) {
autoReimb(docum ent.getElementB yId("miles"+i). value,i)
}
}

function autoReimb(myval ue,myitem){
var nrow = "autoReimb" + myitem;
if(isPositiveIn teger(myvalue)) {
var amount = myvalue * .375;
formatedamt = format(amount)
//eval("document. forms[0]." + nrow + ".value = formatedamt");
document.getEle mentById(nrow). value = formatedamt;
if(isPositiveIn teger(document. forms[0].miles1.value))
var reimb1 = document.forms[0].autoReimb1.val ue;
else var reimb1 = 0;
if(isPositiveIn teger(document. forms[0].miles2.value))
var reimb2 = document.forms[0].autoReimb2.val ue;
else var reimb2 = 0;
if(isPositiveIn teger(document. forms[0].miles3.value))
var reimb3 = document.forms[0].autoReimb3.val ue;
else var reimb3 = 0;
if(isPositiveIn teger(document. forms[0].miles4.value))
var reimb4 = document.forms[0].autoReimb4.val ue;
else var reimb4 = 0;
if(isPositiveIn teger(document. forms[0].miles5.value))
var reimb5 = document.forms[0].autoReimb5.val ue;
else var reimb5 = 0;
if(isPositiveIn teger(document. forms[0].miles6.value))
var reimb6 = document.forms[0].autoReimb6.val ue;
else var reimb6 = 0;
if(isPositiveIn teger(document. forms[0].miles7.value))
var reimb7 = document.forms[0].autoReimb7.val ue;
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(totalaut o);
}
//else eval("document. forms[0]." + nrow + ".value = ''");
else document.getEle mentById(nrow). value = "";
}
function format(amount) {
// { your code }
return amount;
}
function isPositiveInteg er () {
// { your code }
return true;
}
</script>
<style type="text/css">
..rite { text-align:right; width:40px }
</style>
</head>
<body>
<form>
<br><input type="text" name="miles1" value="10" class="rite">
= <input type="text" name="autoReimb 1" value="" class="rite">
<br><input type="text" name="miles2" value="20" class="rite">
= <input type="text" name="autoReimb 2" value="" class="rite">
<br><input type="text" name="miles3" value="30" class="rite">
= <input type="text" name="autoReimb 3" value="" class="rite">
<br><input type="text" name="miles4" value="40" class="rite">
= <input type="text" name="autoReimb 4" value="" class="rite">
<br><input type="text" name="miles5" value="50" class="rite">
= <input type="text" name="autoReimb 5" value="" class="rite">
<br><input type="text" name="miles6" value="60" class="rite">
= <input type="text" name="autoReimb 6" value="" class="rite">
<br><input type="text" name="miles7" value="70" class="rite">
= <input type="text" name="autoReimb 7" value="" class="rite">
<br><input type="text" name="autotot">
<input type="button" value="Calc" onclick="valida te()">
</form>
</body>
</html>
Jul 23 '05 #2
On Fri, 03 Sep 2004 20:54:49 GMT, McKirahan <Ne**@McKirahan .com> wrote:

[snip]
1) Use "document.getEl ementById" 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.valu e * 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
"Michael Winter" <M.******@bluey onder.co.invali d> wrote in message
news:opsdrxjdk4 x13kvk@atlantis ...
On Fri, 03 Sep 2004 20:54:49 GMT, McKirahan <Ne**@McKirahan .com> wrote:

[snip]
1) Use "document.getEl ementById" 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(myval ue,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.valu e * 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
On Sat, 04 Sep 2004 02:25:40 GMT, McKirahan <Ne**@McKirahan .com> wrote:
"Michael Winter" <M.******@bluey onder.co.invali d> wrote in message
news:opsdrxjdk4 x13kvk@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
"Michael Winter" <M.******@bluey onder.co.invali d> wrote in message
news:opsdsiucx5 x13kvk@atlantis ...
On Sat, 04 Sep 2004 02:25:40 GMT, McKirahan <Ne**@McKirahan .com> wrote:
"Michael Winter" <M.******@bluey onder.co.invali d> wrote in message
news:opsdrxjdk4 x13kvk@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]


Anytime eval() usage is brought up in this ng it gets jumped on.

Here's a recent post which discusses "Why is 'eval' evil?".

From: Lasse Reichstein Nielsen (lr*@hotpop.com )
Subject: Re: Why is 'eval' evil?
View: Complete Thread (8 articles)
Original Format
Newsgroups: comp.lang.javas cript
Date: 2004-04-04 16:06:50 PST
Reply Via Newsgroup <re************ ****@please.com > writes:
I don't use eval alot in my scripts - but I do use it - and since I
always out to learn more / improve my javascript skills, I'm curious
why something I thought 'normal' would be considered abnormal.

Can someone put some meat on the bones of 'eval' - its advantages (if
any) and its disadvantages (which seem great).


As you might guess, it's not the first time someone has questionend
the "eval is evil" slogan :) It even made the FAQ.
<URL:http://jibbering.com/faq/#FAQ4_40>

The short argument for not using eval is:
"It's shooting pidgeons with cannons."
Sure, it get's the job done, but it's harder to control and takes a
lot more resources than needed, and when it fails, it fails
spectacularly (read: blows up in your face).

There is (almost) no situation where there isn't another method that
also does the job, and both more efficiently and a lot safer.

With "more efficient" I mean that it uses fewer resources. The "eval"
function works by first turning its argument into a string, then it
parses the string as a Javascript program and finally it evaluates
it. This is a very expensive operation, and the generality of it is
only needed in rare cases that most people writing web pages will
never meet.

With "safer" I mean that it it is less likely to fail spectacularly.
Since eval can execute arbitrary Javascript expressions, passing the
wrong argument can cause arbitrary errors. On a server, using eval on
a user supplied string is a *very* bad idea. On a client, the main
problem is that the error message is harder to connect to the actual
error, and that, e.g., syntax errors in eval'ed code will only be
detected at run time, not when the script is loaded. So: eval
both introduces more possible errors and hides existing errors.

The two most common (mis)uses of eval are:
1) converting strings to numbers.
There are plenty of dedicated functions and operators for just this
problem: parseInt, parseFloat, Number, the prefix plus operator, most
mathematical operators (string*1,strin g/1,string-0). Of these, the
prefix plus is the fastest by a small margin. It is roughly *50* times
faster than using eval (in my browser).

2) accessing properties using a computed name.
Example:
eval("document. images.img"+n+" .src")
Again it is inefficient, here compared to using square-bracket
notation for property access:
document.images['img'+n].src
It is also error prone. There is no syntax check, and if the variable
"n" contains something you didn't expect, then the failure can be
hard to find. If the property is called something that is not an
identifier (typically "foo[]", used by PHP for form controls, or
perhaps "foo1.1"), then the eval method fails completely.

This is what I take as a sign that the author doesn't know the
language very well. Often the reason for using eval like this is
that they don't know about this way to do property access, which
is a fundamental part of the language. Using eval like this is a
crutch that allows them to stagger along, getting something to
run, whereas knowing the language would let them run :)

Then there is the third misuse (which the mentioned calendar program
also sufferend from): throwing in an eval "just for good measure",
even though someone who knows the language can see that it doesn't do
anything. :)

So, eval isn't evil, that's just a good slogan :)

Eval is *very* slow and dangerously error prone!

For *that* reason, it should be avoided in 99.999% of all cases. As
for the remaining two, when you meet them, you'll hopefully know the
language well enough to be able to recognize them.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #6
On Sat, 04 Sep 2004 12:50:28 GMT, McKirahan <Ne**@McKirahan .com> wrote:
"Michael Winter" <M.******@bluey onder.co.invali d> wrote in message
news:opsdsiucx5 x13kvk@atlantis ...
On Sat, 04 Sep 2004 02:25:40 GMT, McKirahan <Ne**@McKirahan .com> wrote:
http://www.jibbering.com/faq/#FAQ4_40


What's that meant to imply?


Anytime eval() usage is brought up in this ng it gets jumped on.


Unless this is purely for the OP's benefit, and I misunderstood, you
haven't grasped my point: I'm not recommending eval (where is an eval call
in my code?), I'm saying that getElementById is not the correct approach.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #7
You guys are my heros. Thank you.
Jul 23 '05 #8
McKirahan wrote:
"Michael Winter" <M.******@bluey onder.co.invali d> wrote [...]:
On Fri, 03 Sep 2004 20:54:49 GMT, McKirahan <Ne**@McKirahan .com> wrote:

[snip]
> 1) Use "document.getEl ementById" 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


You read, but you do not understand.
PointedEars
--
The fat man walks alone at midnight.
Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2624
by: Peter Olcott | last post by:
I created an object that requires access to another objects data, yet have found no good way to pass this data as a parameter because the member function that requires this data must be a binary predicate for std::sort. The only way that I got it to work so far is to make the other object global. Are there any better ways than this? Thanks, Peter Olcott
6
3872
by: bratiskovci | last post by:
1. How do I change the program so that the program does not terminate after completing one conversion. Instead, the program should continue to convert values until the user indicates that he/she wishes to quit. 2. How do you change the structure of the program so that ALL conversion computations are in a function of their own. Input: the user inputs the amount in one of the following forms: # C value # F value # ...
10
1598
by: rh0dium | last post by:
Hi all, Below is a basic threading program. The basic I idea is that I have a function which needs to be run using a queue of data. Early on I specified my function needed to only accept basic parameters ( no postional *args or *kwargs ) but now I am re-writing it and I want to accept these. Is there anyway to determine what parameters are needed by a given function and format the arguments given appropriately. My problem is that I...
3
2052
by: tomPee | last post by:
Hi, I have the following problem: I am trying to make some sort of base class menu that i can then use to derive other menu's from. Those menu's should then be able to interact with each other. And, i have most of the idea figured out and I thought out how i want to do it. But when i started coding i found a slight... difficulty. It might be easy to overcome, but google let me down :( and my own imagination made one happy jump, but...
1
1673
by: manu1001 | last post by:
I've an old C function that calls a function pointed by a global function pointer. How do I get it to call a member function of a class object determined at run-time. It'll be complied in a C++ project of course. My main motive is reusability here. So I don't want to change the C function. Its something like this: void (*foo)(int); class coo {
1
1558
by: MGM | last post by:
Hey everyone, I had a quick problem: I want to be able to send a string of data to a function and have it return that same string but only in alphanumerics. That is to say, if I sent it: "Hello world 1!" It should return: "Helloworld1"
6
1277
by: meetharry19 | last post by:
i m trying to make a function which will calculate size of each node of an ordered statistic tree . here size refers to the number of nodes in left sub tree and right sub tree plus one... but whenever i call this function compiler gives error....and program terminates at line: if(ptr->lc==NULL && ptr->rc==NULL) size(struct node *ptr) { if(root==NULL) {
7
10275
by: KPR1977 | last post by:
Ok, this is a tough one. I need to query “tblRawData” where “fldID” equals “fldLoop” in “tblLoop” and append the results into “tblResults”. If I were to do this exclusively in SQL, it would look something like this: INSERT INTO tblResults ( FldID, SRCTNE, SRYRNE, SRSER2, SRFMLY ) SELECT FldID, SRCTNE, SRYRNE, SRSER2, SRFMLY FROM tblRawData, tblLoop where tblRawData.FldID = tblLoop.fldLoop; Also, you can see this illustrated in my...
6
1669
by: nuken | last post by:
the output looks likenthis:
0
9671
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10433
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10212
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10161
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9035
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4112
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.