Jump to content

# JS Math."middle"

## Recommended Posts

I want to get the value that is in the center between three values.

For example: Math."middle"(34.78, 56.12, 45.54)

Result in: 45.54

How I can do this?

Edited by tiscavalcanti
##### Share on other sites

Three given values? Why not use an exhaustive if-then-else construct?

Your question is oddly worded because you might also be implying the arithmetic average which is 45.45.

##### Share on other sites

You can either write your own function to return the median of an array, or you can use a library that includes one.

##### Share on other sites

You can either write your own function to return the median of an array, or you can use a library that includes one.

http://mathjs.org/docs/reference/functions/median.html

Thanks, but I believe which Math.median() don't work in After Effects Software by expression. I get a error ("Math.median is not a function").

Three given values? Why not use an exhaustive if-then-else construct?

Your question is oddly worded because you might also be implying the arithmetic average which is 45.45.

I could use if/else but, in my case, the values are not fixed, this value that I show is just example.

How i can do this between if/else?

Edited by tiscavalcanti
##### Share on other sites

Again, your question is oddly worded. Are you saying that you want to supply three values to a function and receive the "middle" value back?

```function middle(a,b,c){
if ((a > b && a < c)||(a > c && a < ){
return a;
}else if ((b > a && b < c)||(b > c && b < a)){
return b;
}else if ((c > a && c < ||(c > b && c < a)){
return c;
}else{
return -999; // if a = b or a = c or b = c
}

}//end of function
```
##### Share on other sites

Again, your question is oddly worded. Are you saying that you want to supply three values to a function and receive the "middle" value back?

I want the value of a property of my program, to which this code is entered, is defined by the value of another property that is between two other properties, ie three properties, I do not even want Math.max or Math. min, but the value is between these two.

##### Share on other sites

Thanks, but I believe which Math.median() don't work in After Effects Software by expression. I get a error ("Math.median is not a function").

The website I linked to hosts the library which defines it. It is not built in to Javascript. They wrote their own function to do it, and you can download their library to define that method and whatever else the library includes. Or, like I said, write your own.
##### Share on other sites

And the term you're looking for is the statistical median. It's already a defined term. It's not the "middle", it's the median value.

##### Share on other sites

I want the value of a property of my program, to which this code is entered, is defined by the value of another property that is between two other properties, ie three properties, I do not even want Math.max or Math. min, but the value is between these two.

This would be much simpler if you actually provided an example of what you want this function to do. You seemed to do this in your first post above, but now I'm not sure.

##### Share on other sites

Again, your question is oddly worded. Are you saying that you want to supply three values to a function and receive the "middle" value back?

```function middle(a,b,c){
if ((a > b && a < c)||(a > c && a < ){
return a;
}else if ((b > a && b < c)||(b > c && b < a)){
return b;
}else if ((c > a && c < ||(c > b && c < a)){
return c;
}else{
return -999; // if a = b or a = c or b = c
}

}//end of function
```

Thanks for your time, my friend. This forum like ever, very efficient. Work very well.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
×
• Create New...