Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import flash.events.MouseEvent;
- import fl.controls.TextArea;
- //Declare numbersArray Array and define it as an empty array
- var numbersArray:Array = [];
- //Restrict the input textfields to only numbers
- registerTextField.restrict = "0-9";
- searchTextField.restrict = "0-9";
- //Register
- registerButton.addEventListener( MouseEvent.CLICK, onRegisterClick );
- function onRegisterClick( event:MouseEvent ):void
- {
- //If registerTextField is not empty
- if( registerTextField.length )
- {
- //Push the typed number in the numbersArray
- numbersArray.push( Number( registerTextField.text ) );
- //Update the outputTextArea
- update(registerTextField.text+" registered.");
- //Clear the registerTextField
- registerTextField.text = "";
- //Give registerTextField the focus
- stage.focus = registerTextField;
- }
- };
- //Function which returns a string containing all the numbers
- //separated by line breaks
- function parseNumbers():String
- {
- var sorted:String = "";
- for(var k:String in numbersArray) sorted += String(numbersArray[k]) + "\n";
- return sorted;
- };
- //Ascending
- ascendingButton.addEventListener( MouseEvent.CLICK, onAscendingClick );
- function onAscendingClick( event:MouseEvent ):void
- {
- //If numbersArray is not empty
- if(numbersArray.length)
- {
- for(var i:int = 0; i < numbersArray.length; i++)
- {
- for(var j:int = 0; j < numbersArray.length; j++)
- {
- if(numbersArray[j] > numbersArray[j+1])
- {
- var temp:Number = numbersArray[j];
- numbersArray[j] = numbersArray[j+1];
- numbersArray[j+1] = temp;
- temp = NaN;
- }
- }
- }
- //Update the TextArea
- update("Sorting in ascending order...\n"+parseNumbers());
- }
- };
- //Descending
- descendingButton.addEventListener( MouseEvent.CLICK, onDescendingClick );
- function onDescendingClick( event:MouseEvent ):void
- {
- //If numbersArray is not empty
- if(numbersArray.length)
- {
- for(var i:int = 0; i < numbersArray.length; i++)
- {
- for(var j:int = 0; j < numbersArray.length; j++)
- {
- if(numbersArray[j] < numbersArray[j+1])
- {
- var temp:Number = numbersArray[j];
- numbersArray[j] = numbersArray[j+1];
- numbersArray[j+1] = temp;
- temp = NaN;
- }
- }
- }
- //Update the TextArea
- update("Sorting in descending order...\n"+parseNumbers());
- }
- };
- //Search
- searchButton.addEventListener( MouseEvent.CLICK, onSearchClick );
- function onSearchClick( event:MouseEvent ):void
- {
- //If searchTextField is not empty
- if(searchTextField.length)
- {
- //Search the typed number in numbersArray and return the index
- var index:int = numbersArray.indexOf( Number( searchTextField.text ) );
- //If the number is found and the array is not empty
- if( index > -1 && numbersArray.length )
- {
- //Update the TextArea
- update("Results: " + numbersArray[ index ] + " found at index " + index);
- }
- //else, either the number was not found or the array is empty
- else
- {
- //Update the TextArea
- update("No results.");
- }
- //Clear the searchTextField
- searchTextField.text = "";
- //Give searchTextField focus
- stage.focus = searchTextField;
- }
- };
- //Update function
- function update(string:String = null):void
- {
- //If string is not empty
- if(string)
- {
- //Append the string in the TextArea
- outputTextArea.appendText( String( string ) + "\n" );
- //Update the TextArea's scrollbar position
- outputTextArea.verticalScrollPosition = outputTextArea.maxVerticalScrollPosition;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement