The operators display

For the calculator that I've created, I had until now all the buttons d entry in the display such as the numbers and operators. However for operators when trying to store the value in two variables 'num1' and 'num2', I traced to test whether they worked very well however they came out as "NaN". Here is the code I have so far and the highlighted area is what I'm currently working

import flash.events.MouseEvent;


var btn:Array = new Array();


for(var i = 0; i < 10; i++) {
          var myBtn:Btn = new Btn();
          myBtn.y = 15;
          myBtn.x = i * 100 + 15;
          myBtn.width = 48;
          myBtn.height = 48;
          myBtn.buttonMode = true;
          myBtn.mouseChildren = false;
          myBtn.num = Number(i);
          myBtn.caption.text = String (i);
          addChild(myBtn);
          btn.push(myBtn);
          btn[i].addEventListener(MouseEvent.CLICK,pressNumber);//EVENT TO ADD NUMBERS TO DISPLAY 


  
}
btn[0].y += 370;
btn[0].x += 10;


btn[1].y += 310;
btn[1].x += -90;


btn[2].y += 310;
btn[2].x += -130;


btn[3].y += 310;
btn[3].x += -170;


btn[4].y += 250;
btn[4].x += -390;


btn[5].y += 250;
btn[5].x += -430;


btn[6].y += 250;
btn[6].x += -470;




btn[7].y += 190;
btn[7].x += -690;


btn[8].y += 190;
btn[8].x += -730;


btn[9].y += 190;
btn[9].x += -770;


//OPERATORS//
var operators:Array = new Array();
for(var io:int = 0; io < 6; io++) {
          var opBtn:Btn_operator = new Btn_operator();
          opBtn.width = 48;
          opBtn.height = 48;
          opBtn.buttonMode = true;
          opBtn.mouseChildren = false;
          opBtn.caption_op.text = String (".");
          addChild(opBtn);
          operators.push(opBtn); 
          operators[io].addEventListener(MouseEvent.CLICK, pressOperator);//EVENT TO ADD OPERATORS TO DISPLAY
}


//ADD DOT
var dot:Btn_dot = new Btn_dot();
          dot.width = 48;
          dot.height = 48;
          dot.buttonMode = true;
          dot.mouseChildren = false;
          dot.caption_op.text = String (".");
          addChild(dot);
  


dot.y += 383;
dot.x += 78;
dot.addEventListener(MouseEvent.CLICK, addDot);//EVENT TO ADD DOT TO DISPLAY






//BACKSPACE
var goBack:Btn_backspace = new Btn_backspace();
          goBack.width = 48;
          goBack.height = 48;
          goBack.buttonMode = true;
          goBack.mouseChildren = false;
          goBack.caption_op.text = String ("<--");
          addChild(goBack);
  


goBack.y += 203;
goBack.x += 256;
goBack.addEventListener(MouseEvent.CLICK, backSpace);//EVENT TO GO BACK SPACE IN DISPLAY










//BACKSPACE
var clearAll:Btn_clear = new Btn_clear();
          clearAll.width = 48;
          clearAll.height = 48;
          clearAll.buttonMode = true;
          clearAll.mouseChildren = false;
          clearAll.caption_op.text = String ("C");
          addChild(clearAll);
  


clearAll.y += 323;
clearAll.x += 256;
clearAll.addEventListener(MouseEvent.CLICK, clearFields);//EVENT TO GO BACK SPACE IN DISPLAY








operators[0].y += 383;
operators[0].x += 138;
operators[0].caption_op.text = String("=");




operators[1].y += 383;
operators[1].x += 198;
operators[1].caption_op.text = String("/");




operators[2].y += 323;
operators[2].x += 198;
operators[2].caption_op.text = String("*");


operators[3].y += 263;
operators[3].x += 198;
operators[3].caption_op.text = String("-");


operators[4].y += 203;
operators[4].x += 198;
operators[4].caption_op.text = String("+");


operators[5].y += 263;
operators[5].x += 256;
operators[5].caption_op.text = String("-/+");




//VARIABLE HANDLE OPERATION NOT BUTTON
var operate:String;


//HANDLE FIRST AND SECOND VALUE
var num1:Number;
var num2:Number;


//grouping all buttons in function




//display_txt.text = "0";
//DISPLAYING NUMBERS IN DISPLAY
//var numberEntered:String ="";
function pressNumber(e:MouseEvent):void{
                 display_txt.appendText(e.target.num);
}


//DISPLAY OPERATORS
function pressOperator(event:MouseEvent):void{
          var operatorEntered:String;
          display_txt.appendText(event.currentTarget.caption_op.text);
          //CHECKING FOR VALUES AND STORING NUMBERS
          if(!num1){
                    //CONVERT STRING TO NUMBER
                    num1=Number(display_txt.text);
                    operate = operatorEntered;
                    trace(num1);
                    display_txt.text = "";
  
          }
          else if(!num2){
                    num2 = Number(display_txt.text);
                    performCalculation();
                    operate = operatorEntered;
          }
          }




//CLEARS DISPLAY AREA
function clearFields(event:MouseEvent):void{
          display_txt.text = "";
          num1=NaN;
          num2=NaN;
          }


//ADDS DECIMAL PLACE
function addDot(event:MouseEvent):void{
          if (display_txt.text.indexOf(".")==-1){
                    display_txt.appendText(".");
          }
  
          }
//BACKSPACE DISPLAY AREA
function backSpace(e:MouseEvent):void{
          var temp_str:String = display_txt.text;
          display_txt.text = temp_str.substr(0, (temp_str.length-1)); // Get rid of last character in the string, which in this case is the phantom \r or \n character
}


function performCalculation():void{
  
          switch (operate){
          case "multiply":
                    num1*=num2;
          break;
          case "divide":
                    num1/=num2;
          break;
          case "subtract":
                    num1-=num2;
          break;
          case "add":
                    num1+=num2;
          break;
                    default:
          break;
}
//now that we found out the result
//let's display on the window
display_txt.text=String(num1);
num2=NaN;
  
}



I need to see if the number is stored when I click on operator d. In the trace, it is supposed to display the number entered after I click on an operator, but it displays as "NaN" I guess all of this function has errors. This is the pressOperator function, I work with that

Post edited by: ay5ha

use:

var currentNumber:String ="";
function pressNumber(e:MouseEvent):void{
                 display_txt.appendText(e.target.num);
                 currentNumber+=e.target.num;
}

Tags: Adobe Animate

Similar Questions

Maybe you are looking for

  • Emails have been sent, but I was not yet connected to both.

    2 emails were sent on 11 / 25 at 14:15. I was not connected at the time. I do not keep open e-mail, I close every time that I leave.A single e-mail copied a zip file that I had sent to my nephew in an e-mail of 10/05/2011; He sent me it. The e-mail s

  • HP Officejet Pro 8600 Plus - printing of Documents for glass copy and charger slowly

    We bought this printer a few months back, and everything was working great. Yesterday afternoon, the COPY works, the high plateau of the loader page or the scanning tray, from work extremely slow. If I put a document on the glass and do a preview, it

  • "You must format the disk in drive g before you can use it"

    "You need to format the disk in drive g before you can use it... "Really crappy to get Msg. I have a Corsair 16 GB FAT drive. I got the disc from my Vista computer and go to a XP machine safely. Aaarrrggghhh! New tips on how to solve this problem?

  • 1330 error when you try to install the software main street

    Receive the following error message when you try to install the update of the software of Boulevard of Main Street Software, Inc. 2011 on a machine running Windows Vista 64 bit HP. File 1330.A error required cannot be installed because the file E:\Bl

  • 3050 J611a has stopped free wireless

    I got my printer put in place and works wirelessly for several months, then all of a sudden it stopped - I don't think that something has changed on my network, it seems to be the printer. I tried to re-login via WPS, but the reports WPS printer erro