0

I'm trying to make some simple calculations using input from the user.

I have two input fields, a button and a dynamic textfield for showing results. What I want is for the user to write a number in the first field, then a percent in the next field. Then he/she will click the button, and the correct calculated numer will show in the result field.

I'm using this code on the button:

on (release){

var revenue:Number =input1_txt.text;
var revenuegrowth:Number = input2_txt.text;
var growth:Number =revenue * revenuegrowth / 100;

}

The problem is, it will not work the first time I click the button. It only turns out with NaN - not a number - in the result field. (at the second click on the button it works just fine)

Any ideas on how to make it work on the first click also?

2 Answers2

0

Try to convert your input to Number

Update : Added another field named input3_txt to display the result or an error message and a test to check if inputs values are numbers :

on (release){
  var revenue:Number = Number(input1_txt.text);
  var revenuegrowth:Number = Number(input2_txt.text);
  if (isNaN(revenue) ===false & isNaN(revenuegrowth)  ===false) {
    var growth:Number =revenue * revenuegrowth / 100;
    input3_txt.text = growth + '%'
  } else {
     input3_txt.text = 'Error';
  }
}
RafH
  • 4,504
  • 2
  • 23
  • 23
  • Thanks. But didn't work. Then I got NaN not only on first click, but on second also. Maybe I need some kind of loop that simulates the first click? – user2823517 Oct 01 '13 at 10:53
  • You get NaN if input1_txt or/and input2_txt doesnt contains a number otherwise it works. – RafH Oct 01 '13 at 17:30
0

I got it to work by repeating the code two times within the same click. Probably not the best soultuion, but at least it works:

on (release){

var revenue:Number =input1_txt.text;
var revenuegrowth:Number = input2_txt.text;
var growth:Number =revenue * revenuegrowth / 100;
var revenue:Number =input1_txt.text;
var revenuegrowth:Number = input2_txt.text;
var growth:Number =revenue * revenuegrowth / 100;

}