Solved

Javascript Math.Max Problem - Returning NaN

Posted on 2011-03-22
3
1,300 Views
Last Modified: 2012-05-11
While looping through some XML using JQuery I am constructing a grouping of numbers.   From those numbers I am trying to acquire which of the numbers is the largest.  For some reason, it is returning NaN when I use the Math.max function.  Each number is no larger than 6 digits at most.

JQuery Loop to Construct Grouping
var snewmileagegroup = 0;
$(this).find('XMLChild').each(function(){
	snewmileagegroup = snewmileagegroup + ',' + parseFloat($(this).find('Mileage').text());
});

Open in new window


Result: 0,36000,48000,65000

Now I taking the resulting variable and apply the Math.max function to it expecting the return of 65,000.  Problem is that I am getting NaN returned back instead.
var nmg = Math.max(snewmileagegroup);
alert(nmg);

Open in new window


My suspicion is that I am not creating the collection of numbers correctly by concatenating ','.  Any help will be greatly appreciated.
0
Comment
Question by:joshaxton
  • 2
3 Comments
 
LVL 9

Expert Comment

by:jkofte
ID: 35190735
I don't know if you can send any collection to the max function.

can you try by sending them seperately like max(a,b,c)
0
 

Accepted Solution

by:
joshaxton earned 0 total points
ID: 35190783
I should have waited to post this as I solved the problem.  Instead of creating a grouping I created an array of numbers.  So the code looks like this now.

var snewmileagegroup = new Array();
var i = 0;
$(this).find('XMLChild').each(function(){
	snewmileagegroup[i] = parseFloat($(this).find('Mileage').text());
        i=i+1;
});

var nmg = Math.max.apply(Math, snewmileagegroup);
alert(nmg);

Open in new window

0
 

Author Closing Comment

by:joshaxton
ID: 35225352
I figured out the solution on my own and therefore I am posting the solution.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now