| Find the Largest of 3 Values
• | | | |
 | Find the Largest of 3 Values
by Jon Smith on Jun 26, 2012 at 8:53:00 pm |
I trying to figure out how to find the largest of 3 values. I got fairly close using if/else statements but it always breaks somewhere. Ideally I would like to get the 3 values as small, med and large. Thanks.
Here's where I left off:
if (a > b) {
if (a > c){
v = a
}
else {
if (b > c){
if (b > a)
v = b
}
else {
if (c > a)
v = c
}
}
}
else {
v = c
}
| | | | |
• | | | |  | Re: Find the Largest of 3 Values by Jon Smith on Jun 26, 2012 at 9:03:02 pm |
Okay, after taking a deep breath and diving back in, I've figured out how to get the largest value.
if (a > b) {
if (a > c){
v = a
}
else {
if (c > b){
v = c
}
}
}
else {
if (b>c){
v = b
}
else{
v = c
}
}
| | | | |
• | | | |  | Re: Find the Largest of 3 Values by Kevin Camp on Jun 26, 2012 at 9:16:08 pm |
i think you could do it with this:
Math.max(Math.max(a,b),c);
if you needed more values, you'd need to embed them in more Math.max() functions, ex:
Math.max(Math.max(Math.max(a,b),c),d);
Kevin Camp
Senior Designer
KCPQ, KMYQ & KRCW
| | | | |
• | | | |  | Re: Find the Largest of 3 Values by Dan Ebberts on Jun 26, 2012 at 9:34:02 pm |
Math.max is designed to take as many parameters as you want to throw at it. This should work:
Math.max(a,b,c)
I seem to recall there was a version of AE where this didn't quite work as advertised, but I'd say try it and see if it works for you.
Dan
| | | | |
• | | | |  | Re: Find the Largest of 3 Values by Kevin Camp on Jun 26, 2012 at 9:47:22 pm |
you know, i've always thought it should work that way, but never tried it -- though i've rarely needed it...
thanks.
Kevin Camp
Senior Designer
KCPQ, KMYQ & KRCW
| | | | |
• | | | |  | Re: Find the Largest of 3 Values by Kevin Camp on Jun 26, 2012 at 9:50:22 pm |
well... it doesn't seem to work as advertised in cs4. it only seemed to compare the first 2 values.
Kevin Camp
Senior Designer
KCPQ, KMYQ & KRCW
| | | | |
• | | | |  | Re: Find the Largest of 3 Values by Dan Ebberts on Jun 26, 2012 at 9:59:01 pm |
Yeah, it looks like it didn't get fixed until CS6. Safer to do it your way.
Dan
| | | | |
• | | | |  | Re: Find the Largest of 3 Values by Jon Smith on Jun 26, 2012 at 9:50:44 pm |
Thanks a bunch. That's much simpler. Looks like you do need to nest it the way Kevin has it. Any thoughts on how to get the middle value?
| | | | |
• | | | |  | Re: Find the Largest of 3 Values by Kevin Camp on Jun 26, 2012 at 10:06:22 pm |
i think this would work...
vals = [a,b,c];
max = Math.max(Math.max(a,b),c);
min = Math.min(Math.min(a,b),c);
for (i = 0; i < vals.length; i++){
if (vals[i] > min && vals[i] < max) mid = vals[i];
}
mid
Kevin Camp
Senior Designer
KCPQ, KMYQ & KRCW
| | | | |
• | | | |  | Re: Find the Largest of 3 Values by Jon Smith on Jun 26, 2012 at 10:27:41 pm |
Seems to work unless there are equal values then the expression breaks. Is there a way around that?
| | | | |
• | | | |  | Re: Find the Largest of 3 Values by Xavier Gomez on Jun 27, 2012 at 12:50:42 pm |
Try this:
max = Math.max(Math.max(a,b),c);
min = Math.min(Math.min(a,b),c);
if (a < max){
if (a > min) mid=a else mid=Math.min(b,c);
}
else {mid = Math.max(b,c)};
mid
It will hardly generalize to higher number of parameters but for just 3 it is ok.
| | | | |
• | | | |  | Re: Find the Largest of 3 Values by Dan Ebberts on Jun 27, 2012 at 1:18:17 pm |
I think this works too:
Math.min(Math.min(Math.max(a,b),Math.max(a,c)),Math.max(b,c))
Dan
| | | | |
• | | | |  | Re: Find the Largest of 3 Values by Walter Soyka on Jun 30, 2012 at 1:14:09 am |
Here's an alternate approach, using arrays and the sort() method. First, stuff each of the values into an unsorted array. Next, sort the array. Finally, return the value of the last item in the array, which will be the highest value because we just sorted it.
unsortedArray = [a,b,c];
sortedArray = unsortedArray.sort();
sortedArray[sortedArray.length-1];
To get the middle value, replace the final line with: sortedArray[Math.round(sortedArray.length/2)-1];
To get the lowest value, replace the final line with: sortedArray[0];
You can shorten it, too, removing the assignment:
myArray = [a,b,c];
myArray.sort()[sortedArray.length-1];
Walter Soyka
Principal & Designer at Keen Live
Motion Graphics, Widescreen Events, Presentation Design, and Consulting
RenderBreak Blog - What I'm thinking when my workstation's thinking
Creative Cow Forum Host: Live & Stage Events
| | | | |
| |
|