The problem is pretty common in things like binary searches as well. I’ve run 
into invalid indexes more than once.

I do agree that we generally don’t want to add the extra code though…

Harbs

> On Nov 30, 2018, at 4:10 PM, Alex Harui <[email protected]> wrote:
> 
> Hi,
> 
> There is some old Flex code that does this:
> 
>    var r:int = Math.random() * 5;
>    var foo:String = someArray[r];
> 
> Flash will automatically run the equivalent of Math.floor() so r will be 
> assigned 0, 1, 2, 3, or 4 and choose an element from the array.
> 
> In JS right now, r will be a floating point number and thus the array lookup 
> will fail.  The compiler can detect this (in many cases) so we could wrap 
> these assignments like this:
> 
>    var r = Math.floor(Math.random() * 5);
> 
> But this will result in Math.floor showing up in more places than I think we 
> want.  That's because, in JS, XMLList.length() is a Number, and so is 
> parseInt().  So, I'm tempted to not do the coercion and folks will have to 
> find this bug in their code and add the Math.floor() themselves.  I think 
> this is a pretty rare case.
> 
> Thoughts?
> -Alex
> 

Reply via email to