Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Reply
 
LinkBack Thread Tools Display Modes
Old 25-08-2011, 06:30 PM   #1 (permalink)
Broken In
 
clerkman1612's Avatar
 
Join Date: Aug 2010
Posts: 105
Arrow program for finding maximum n minimum in javascript arrays?


I want to know about a program which find itself the maximum and minimum numbers in data of arrays(JavaScript)? I m using adobe dreamweaver.
I m very confused. Plz tell me.
take example
var a = new Array
a[0]=1
a[1]=2
a[2]=3
a[3]=4
a[4]=5

now here we have to find what is maximum and mininum no in this arrays?
Obviously answer is 5 and 1 respectively.
Thanks in Advance
__________________
Phenom II * 4 945 || MSI 880GM-E41 || 2 * 2 GB DDR3 RAM || MSI HD 5770 Hawk 1 GB || CM Elite 310 || 1 T.B Seagate Hard Disk || 19" Dell ||Win7 64Bit :clap2: :doublethumb: :nanananana:
clerkman1612 is offline   Reply With Quote
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 25-08-2011, 09:43 PM   #2 (permalink)
Simply a DIGITian
 
krishnandu.sarkar's Avatar
 
Join Date: Nov 2007
Location: Kolkata
Posts: 2,942
Default Re: program for finding maximum n minimum in javascript arrays?

Code:
Array.prototype.max = function() {
var max = this[0];
var len = this.length;
for (var i = 1; i < len; i++) if (this[i] > max) max = this[i];
return max;
}

Array.prototype.min = function() {
var min = this[0];
var len = this.length;
for (var i = 1; i < len; i++) if (this[i] < min) min = this[i];
return min;
}
OR

Code:
Array.prototype.max = function(){
	return Math.max.apply({},this)
}

Array.prototype.min = function(){
	return Math.min.apply({},this)
}
Call the functions using...
Code:
alert(a.max());
where a is your array...
__________________
  • Read The Forum RULES First.
  • Before PM'ing Or Asking Any Questions To Any Mod Read The FAQ's
  • Before Starting A New Thread Read The STICKY THREADS First
  • Before Participating In Bazaar Section Read The BAZAAR RULES
krishnandu.sarkar is online now   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by Charan
- by Sarath
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 12:33 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2