2 closest numbers to 0


/ Published in: ActionScript 3
Save to your folder(s)

Looking forward for the most optimized solution for the same


Copy this code and paste it in your HTML
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
  3. <mx:Script>
  4. <![CDATA[
  5. public var arr:Array = new Array("-23", "-12", "45", "22", "-42", "87", "97", "13","24","-10", "-2", "5")
  6. public function init():void{
  7. arr.sort(Array.NUMERIC);
  8. for(var i=0;i<arr.length;i++){
  9. txt.text += "\n "+arr[i];
  10. if(arr[i]<0) {
  11. var min:Number = arr[i];
  12. }
  13. if(arr[arr.length -i-1]>0){
  14. var max:Number = arr[arr.length -i-1];
  15. }
  16. }
  17. txt.text += "\n closet +ive number to 0>> "+max+" << closest -ive number to 0 >>"+min;
  18. }
  19. ]]>
  20. </mx:Script>
  21. <mx:TextArea x="118" y="57" width="553" height="412" id="txt"/>
  22. </mx:Application>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.