Quadratic equation in ECMAScript

Example for versions Adobe Flex 3.4

When Main() is called, it contains a small routing to initialize the text fields and display labels.

The example is constructed from four text fields, not counting the clickable button or static text. It takes input from three editable text fields (identified by TextFieldType.INPUT), and places the result in the output text field.

The example may be modified to update the result instantly, but this one requires clicking “Calculate” to get the result.

package 
{
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	
	/**
	 * ...
	 * @author Raymond Martineau
	 */
	public class Main extends Sprite 
	{
		var tf_a:TextField;
		var tf_b:TextField;
		var tf_c:TextField;
		var tf_result:TextField;
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}

		/**
		 * This function initializes the display elements. 
		 */
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			// entry point
			tf_a = new TextField();
			tf_a.text = "1";
			tf_a.x = 40;
			tf_a.y = 10;
			tf_a.height = 20;
			tf_a.type = TextFieldType.INPUT;
			tf_a.border = true;
			this.addChild(tf_a);
		
			
			tf_b = new TextField();
			tf_b.text = "1";
			tf_b.x = 40;
			tf_b.y = 30;
			tf_b.height = 20;
			tf_b.type = TextFieldType.INPUT;
			tf_b.border = true;
			this.addChild(tf_b);
			
			tf_c = new TextField();
			tf_c.text = "0";
			tf_c.x = 40;
			tf_c.y = 50;
			tf_c.height = 20;
			tf_c.type = TextFieldType.INPUT;
			tf_c.border = true;
			this.addChild(tf_c);

			/** 
			 * If desired, you could add the following line to update
			 * the calculation whenever the value is changed
			 * 
			 *   tf_a.addEventListener(Event.CHANGE, changed);
			 *   tf_b.addEventListener(Event.CHANGE, changed);
			 *   tf_c.addEventListener(Event.CHANGE, changed);
			 * 
			 * In this example, it's activated by clicking the calculate button.
			 */
			
			var tf:TextField;
			tf = new TextField();
			tf.text = "A = ";
			tf.x = 10;
			tf.y = 10;
			tf.width = 29;
			tf.height = 20;
			tf.borderColor = 255;
			this.addChild(tf);
			
			tf = new TextField();
			tf.text = "B = ";
			tf.x = 10;
			tf.y = 30;
			tf.width = 29;
			tf.height = 20;
			this.addChild(tf);

			tf = new TextField();
			tf.text = "C = ";
			tf.x = 10;
			tf.y = 50;
			tf.width = 29;
			tf.height = 20;
			this.addChild(tf);

			var btn:TextField = new TextField();
			btn.x = 40
			btn.y = 80
			btn.width = 80
			btn.height = 20
			btn.text = "Calculate"
			btn.border = true;
			btn.backgroundColor = 0xcccccc;
			btn.background = true;
			btn.selectable = false;
			this.addChild(btn);			
			
			btn.addEventListener(MouseEvent.CLICK, changed);
			
			tf_result = new TextField();
			tf_result.x = 10;
			tf_result.y = 110;
			tf_result.width = 400;
			this.addChild(tf_result);

			updateResult();
		}

		private function changed(e:Event = null)
		{
			updateResult();
		}
		
		private function updateResult()
		{
			var a:Number, b:Number, c:Number, d:Number;
			a = Number(tf_a.text); b = Number(tf_b.text); c = Number(tf_c.text);
			
			d = b * b - 4 * a * c;
			if (a == 0)
			{
				tf_result.text = "Not a quadratic equation";
			}
			else if (d == 0)
			{
				tf_result.text = "One root: " + ( -b / (2 * a));
			}
			else if (d>0)
			{
				tf_result.text = "Two roots: " + (( -b + Math.sqrt(d)) / (2*a)) + ", " + (( -b - Math.sqrt(d)) / (2*a));
			} else if (d < 0)
			{
				var e:Number = -b / 2 * a;
				var f:Number = Math.sqrt( -d) / 2 * a;
				if (f < 0) f = -f;
				tf_result.text = "Two roots: (" + e + ", " + f + "), (" + e + " - " + f + ")";
			}
			
			return 0; 
		}
		
	}
	
}