# ActionScript 3

Currently Being Moderated

## Defining a variable's value, using a conditional, on a sliding scale?

### Jul 16, 2012 12:56 PM

Basically what i'm trying to do is assign the variable "upCharge" a number base on the Variable "numberOfSheets". The only problem is that i need to assign a different variable for each sheet up to 6000. Everything over 6000 will have the same variable.

Anybody have any idea how to write something like this without having to list each sheet individually?

Here is an example of my code...

function calculateUpCharge():void{

if(numberOfSheets=1) {

upCharge = Number (20);

}

else if (numberOfSheets=2) {

upCharge = Number (19.5);

}

else if (numberOfSheets=3) {

upCharge = Number (19);

}

else if (numberOfSheets=4) {

upCharge = Number (18.5);

}

else if (numberOfSheets=5) {

upCharge = Number (18);

}}

Replies
• 62,194 posts
Jul 21, 2002
Currently Being Moderated
Jul 16, 2012 1:11 PM   in reply to pandpinc

:

function calculateUpCharge():void{

upCharge=(41-numberOfSheets)/2;

upCharge=Math.max(upCharge,(41-6000)/2);

}

|
Mark as:
• 62,194 posts
Jul 21, 2002
Currently Being Moderated
Jul 16, 2012 3:53 PM   in reply to pandpinc

is that a linear function?  if yes, use:

var m:Number;

var b:Number;

paramF(6000,2,1,20);

function calculateUpCharge():void{

upCharge=m*numberOfSheets+b

upCharge=Math.max(upCharge,m*6000+b;

}

function paramF(x1:Number,y1:Number,x2:Number,y2:Number):void{

m=(y1-y2)/(x1-x2);

b=y1-m*x1;

}

|
Mark as:
• 62,194 posts
Jul 21, 2002
Currently Being Moderated
Jul 16, 2012 5:35 PM   in reply to pandpinc

you're welcome.

|
Mark as:
Actions

#### More Like This

• Retrieving data ...

#### Answers + Points = Status

• 10 points awarded for Correct Answers