Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Finding Max and Min of a number array...

Guest
Apr 29, 2010 Apr 29, 2010

So my array has 5 numbers in it and also the ability to add more to it as the user sees fit. I want to be able to display the max and min number in the array. I made a loop but i am not sure how to make it select the highest or lowest number automatically without me selecting it manually by typing it in.

This is my code for determining this:

// Determine Min and Max Mark
function maxandminMark(){
var markIndex:int;
markIndex=(marks.indexOf(marks));
if (markIndex != -1)
{
  for (var i = markIndex; i < marks.length; i++)
  {
   marks = marks[i+1];
  }
  trace(maxandminMark);
}
}
}

TOPICS
ActionScript
1.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Apr 29, 2010 Apr 29, 2010

Here's a couple of ways...

1) Walk thru the array comparing values

var numArray:Array =  new Array(38,11,45,7,14,9,67,28);
var minNum:Number;
var maxNum:Number;
 
if(numArray.length > 0){
minNum = maxNum = numArray[0];
}
 
for(var i:uint=1; i< numArray.length; i++){
if(numArray < minNum){
  minNum = numArray;
} else if(numArray > maxNum){
  maxNum = numArray;
}
}
 
trace(minNum,maxNum);

2)  Sort the array numerically and pull out the first and last elements

numArray.sort(Array.NUMERIC);

trace(numArray[0],numA

...
Translate
LEGEND ,
Apr 29, 2010 Apr 29, 2010

Here's a couple of ways...

1) Walk thru the array comparing values

var numArray:Array =  new Array(38,11,45,7,14,9,67,28);
var minNum:Number;
var maxNum:Number;
 
if(numArray.length > 0){
minNum = maxNum = numArray[0];
}
 
for(var i:uint=1; i< numArray.length; i++){
if(numArray < minNum){
  minNum = numArray;
} else if(numArray > maxNum){
  maxNum = numArray;
}
}
 
trace(minNum,maxNum);

2)  Sort the array numerically and pull out the first and last elements

numArray.sort(Array.NUMERIC);

trace(numArray[0],numArray[numArray.length-1]);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 29, 2010 Apr 29, 2010

A simple approach but I hope it helps.


var myArray:Array = [200,899,320,10,569];
var largest:Number = myArray[0];
var smallest:Number = myArray[0];
for (var ii = 0; ii<myArray.length; ii++) {
largest = (largest < myArray[ii])?myArray[ii]:largest;
smallest = (smallest > myArray[ii])?myArray[ii]:smallest;
}

trace ("largest = "+largest);
trace ("smallest = "+smallest);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 29, 2010 Apr 29, 2010
LATEST

You can use the Math package. Like so:

var arr:Array = [12,4993,14,2345,190];

var min:Number = Math.min.apply(null,arr);

var max:Number = Math.max.apply(null,arr);


trace(min, max)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines