Strings in ActionScript

Type a sentence and press enter to create one anagram.

Get Adobe Flash player

stage.addEventListener(KeyboardEvent.KEY_DOWN,scramble);
function scramble(e:KeyboardEvent):void{
	if(e.keyCode==13) { // a keycode of 13 means they pressed enter
		var s:String=txtInput.text;
		var letters:Array=s.split(""); //make an array of the letters
		var i,j:int;
		var temp:String;
		for(i=0;i<letters.length;i++) { //each letter
			j=Math.random()*letters.length; //select position of a random letter
			//swap letters i and j
			temp=letters[i]; //store letter[i] 
			letters[i]=letters[j]; //replace letter[i] with [j]
			letters[j]=temp; //put former letter [i] in [j]
		} //loop to scramble
		var s2:String=""; //start with a null string
		for(i=0;i<letters.length;i++) { //add each letter of the scrambled array
			s2=s2+letters[i];
		} //loop to concatenate the scrambled letters
		txtOutput.text=s2; //display the anagram
	} //pressed enter
} //scramble
Download the movie.

NEXT: Cryptograms