#1242 new
Corlynn

Enumerable#min() unexpected behaviour with parseInt

Reported by Corlynn | April 19th, 2011 @ 09:21 PM

I was trying to use min to return the lowest value in an array of numbers, but since the #'s I was getting were being returned as text, I tried passing them through parseInt. This produced very unexpected (and wrong) results.
I've distilled this down to a few simple test cases to demonstrate the odd behaviour.

[10,10].min(parseInt) returns 10

["10","10"].min(parseInt) returns 10

["10","10","10"].min() returns "10"

["10","10","10"].min( parseInt ) returns 2

[10,10,10].min( parseInt ) returns 2

["10","10","10"].min( function(i) { return parseInt(i) } ) returns 10

Here's where things get REALLY weird...

[10,10,1].min(parseInt) returns 1

[10,10,2].min(parseInt) returns 10

[10,10,11].min(parseInt) returns 3

[10,10,12].min(parseInt) returns 1

[5, 7, 10].min(parseInt) returns 2

[15, 27, 9].min(parseInt) returns 15

... and on and on.

Near as I can tell, the following patterns apply. If the 3rd digit is a 10, returns 2. If it's a 1, returns 1. If it's any other single digit, returns whatever the first number is, regardless of size.

That alone was enough to make me sure this was doing something crazy. I've tried to follow the code through to find what's causing this, but I was unsuccessful.

Tested on IE8, Safari 5, Chrome 10

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

The Prototype JavaScript library.

Shared Ticket Bins

People watching this ticket

Pages