Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

conditionally adding numbers 1

Status
Not open for further replies.

jefargrafx

Instructor
May 24, 2001
273
US
okay this works

this.onEnterFrame = function () {
Number(this.BG=(Number(this.BG10)+Number(this.BG01)+Number(this.BG02)+Number(this.BG03)+Number(this.BG04)+Number(this.BG05)+Number(this.BG06)+Number(this.BG07)+Number(this.BG08)+Number(this.BG09)));
}

however I don't have a clue on how to only add a number if a field meets a criteria?

if (this.BG01 <= 10) {
then add it
} else {
not
}

anyone ever do this before?

jef
 
3 fields bg1 bg2 bg3

total = 0;

if (BG1>10){
total = total +bg1;
}
if (BG2>10){
total = total +bg2;
}
if (BG3>10){
total = total +bg3;
}
laborious but that will work
 
I see,

would this approach also work with multiple fields that are more than 10,

if any o f9 fields are more than 10, I do not waht to add them into the total?


what do you think

jef
 
work with any number of fields and any condition just add more fields and alter the if condition to suit.
 
here it is thanks to bill!


this.onEnterFrame = function () {
BG = 0;
if (BG01 < 7499.9){
BG = BG +Number(BG01);
}
if (BG02 < 7499.9){
BG = BG +Number(BG02);
}
if (BG03 < 7499.9){
BG = BG +Number(BG03);
}
if (BG04 < 7499.9){
BG = BG +Number(BG04);
}
if (BG05 < 7499.9){
BG = BG +Number(BG05);
}
if (BG06 < 7499.9){
BG = BG +Number(BG06);
}
if (BG07 < 7499.9){
BG = BG +Number(BG07);
}
if (BG08 < 7499.9){
BG = BG +Number(BG08);
}
if (BG09 < 7499.9){
BG = BG +Number(BG09);
}
}

one cookie for bill

thanks dude

jef
 
Here's a more compact version that does the same thing.
Code:
this.onEnterFrame = function() {
	BG = 0;
	for (var i = 0; i<10; i++) {
		val = Number(this['BG0'+i]);
		if (val<7499.9) {
			BG += val;
		}
	}
};
 
this will cycle throguh all ten field like bills does.

my problem is that I need to remove one or more field from the addtion if they are greater than 7499.9

let me know

jef
 
where as I don't get any syntac errors, this still doesn't
work

yall know why?

Number(this.youlegMC.totaltabsMC.TotalPVMC.BG = 0);
if (Number(this.youlegMC.newleg01MC.personalPVMC.BG10) < 7499.9){
Number(this.youlegMC.totaltabsMC.TotalPVMC.BG=(Number(this.youlegMC.totaltabsMC.TotalPVMC.BG)+Number(this.youlegMC.newleg01MC.personalPVMC.BG10)));
}
for (var i = 0; i<9; i++) {
val = Number(this['.newleg0'+i].MC.personalPVMC['.BG0'+i]);
if (val<7499.9) {
this.youlegMC.totaltabsMC.TotalPVMC.BG += val;
}
}

each field is in a differnet leg and they have different BG00 numbers? 1-9

leg 10 is the users leg

let me know

jef
 
Couple of things...

Number(this.youlegMC.totaltabsMC.TotalPVMC.BG = 0);

...is a bit weird - you don't need Number here, its purpose is to convert string values etc into numbers (for instance if you load values in from a text file they will be interpreted as strings so you can use Number to explicitly state that they should be treated as numbers) - in this case since you're explicitly setting the value to zero it's already a number. It shouldn't break your code but it's burning CPU you could use elsewhere. Likewise when you use it later on in the 'if' statement.

'i' is never reaching 9 - is that deliberate? You set a value for 10 early on and the loop only gets as far as 8 because it's set as <9 not <=9.

val = Number(this['.newleg0'+i].MC.personalPVMC['.BG0'+i]);

...is probably the main problem as it's going to come out as this with double dots when the loop runs:

this..newleg01.MC.personalPVMC..BG01;

Lose the extra dots and it should be okay.

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top