header
ask question
Click here to ask Question Now Its free No registration required. Flash, Flex, Flash Media Server, ActionScript,Adobe Air. Most questions receive a response in an hour.
Shridhar Kulkarni
Points:0
Posts:0

3/24/2011 12:36:50 AM

Title: Finding Closer Number In ArrayCollection


Hello,
My Task is, I having number say 7 now I am having array collection say(5,10,15,20), Now I provide this array collection to combobox,. so when there is number 7 I want to find closest number from array collection and and bring combobox.selectedItem to that closest number( here 5)

So is there any function or API which helps me in this???

Please Help ..!



1
Rex
Points: 1280
Posts:0
3/26/2011 11:52:24 AM



Use the below function in actionscript 3 to find the nearest or closest number in an array. You need to pass the array and the number as a parameter to the below function


//to call the function pass the array and the number to search 
nearestNumber(myArray,7);

//use this function to find the closest or nearest number in the array
private function nearestNumber(myArr:Array, num:Number):Number {
	var nearNum:Number;
	var oldDifference:Number;
	for (var x:int=0; x<myArr.length; x++) {
		var newDifference:Number = Math.abs(Number(myArr[x]) - num);
		if (newDifference <= oldDifference) {
			oldDifference  = newDifference;
			nearNum = myArr[x];
		}
	}
	return nearNum;
}


2
Halley
Points: 0
Posts:0
3/28/2011 2:31:38 AM



thanks that worked pretty well for me


Post your Reply
Name  

Email

Type your Reply or Answer

Are you human? What is 2+4 



Members Login

Email  
Password
Forgot Password





This website focus on: Flash | Flex | FMS | RED5 | WOWZA | Flash Media Server | Adobe AIR | ActionScript,Flash Solutions | Flash Question | Flash Answers | Flash Developers | Flash Problem, Flash Help, Flash bugs, Flash workaround | Flash Blog | Flex Question Answers | Flash Forum | Flex Development | Actionscript development | Flash development | Adobe AIR development
Copyright © 2008 AskMeFlash.com. All rights reserved. Privacy Policy | Terms & Conditions