Sombody please explain to me why the following script:
{
var test = 0;
test = Math.max (8,26,22);
alert (test);
}
outputs "22" in after effects CS5.
am i just missing something really simple or is this a bug?
the same code in a browser works fine.
am i just missing something really simple
Yes, you are. You are simply assigning the value 3 times over rather than declaring an array and naturally the variable only holds the last assigned value. Arrays belong in rectangular brackets, so your declaration needs to look like Math.max([8,26,22]). Read up on JavaScript fundamentals rather than prematurely calling things a bug when they aren't.
Mylenium
Math.max takes a series of integers as arguments, not an array.
If you feed it an array it returns a "NaN" error.
the exact some code evaluated in a browser works fine:
http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_max < change the Math.max to (8,26,22) instead of (5,10)
....
correction - not integers, it should work with floats too - i'm just not using ints in this case
I thought that too, but if its only taking 2 arguments, why would 8,26,22 return 22?
It would have to be taking the first and last arguments, you would think it would take the first 2 and ignore the rest.
For this script i never need to compare anything different then 3 numbers, so i just wrote a function that does that.
But Yea - im thinking something is definitely wrong here, seeing as the exact same code works in every browser ive tested, but not in AE
Yes, but it doesn't work in CS5.
I noticed that in CS5 the result of the Math.max function depends on the order of the arguments as well as their values, but I always only use it with 2 arguments so I never really cared.
It looks like each value is compared to the first one, and the value returned is the last argument found that is greater than the first one.
So Math.max(1.5, 4, 3, 2, 1) will return 2 because the arguments following are lower than 1.5
So Math.max(2.5, 4, 3, 2, 1) will return 3 because the arguments following are lower than 2.5
and Math.max(2.5, 3, 4, 2, 1) will return 4 because the arguments following are lower than 2.5
So it's safer to do what Dan suggested and break the max calculations with a loop. something like:
var values = [ ... an array of values ... ];
var maxValue = values[0];
for (var i=1; i<values.length; i++)
{
maxValue = Math.max(maxValue, values[i]);
}
North America
Europe, Middle East and Africa
Asia Pacific