0

I seem to have the right syntax but i keep getting a NaN value, i believe it has something to do with the propagation of the array, please could you check the code and see where i am going wrong.

var _array = new Array();

var _first = 1;
var _second = 4;

$('#con > div').each(function(index){

    var _data = $(this).css('left').split('px')[0];
    var _class = $(this).attr('class').split(' ')[0];

    _array[_first] = [_data];
    //_array[_second] = _class;

    _first++;
    _second++;

    if(index == 2){
        _first = 1;
        _second = 4;

        for(var i = 0; i < _array.length; i++)
        console.log(_array[i]);

        var large = Math.max.apply(Math, _array);
        console.log(large);

    }
});

Thanks

http://jsfiddle.net/LTMbr/1/

2
  • 1
    Note that your variable _array is not an array of numbers, it is an array of arrays because you have square brackets around _data when you say _array[_first] = [_data]; (unless that was just a typo in your question). Commented Oct 4, 2011 at 14:01
  • Thanks for the mention i realized that and removed it! Commented Oct 4, 2011 at 14:02

2 Answers 2

2

So, I modified it a bit to see - http://jsfiddle.net/LTMbr/4/ and your _array[0] is undefined. So that's why your call to Math.max is returning NaN.

Sign up to request clarification or add additional context in comments.

Comments

2

The problem is that you are populating your array starting at index 1. If any of the arguments to Math.max cannot be converted to a number, NaN is returned
This is the case because _array[0] is undefined

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.