4 Replies Latest reply on Oct 30, 2011 8:03 PM by John Hall

    New to AS and stuck on IF statements


      I am trying to make a currency converter and Im using IFs to determine the from and to currency and then make the appropriate calculation. So far, I cant get anything to work. Here is the original code that I was using (but didnt work):


                     var a:String=String (OriginCurrenyCode.text)

                     var b:String=String (DestinationCurrencyCode.text)

                     var c:Number=Number (AmountToBeConverted.text)


                     if (a == "USD"){


                          if (b == "GBP"){


                               var ab:Number=Number (c*0.60)


                               ConvertedAmount.text = String(ab)



                          else if (b == "EUR"){


                               var ac:Number=Number (c*0.71)


                               ConvertedAmount.text = String(ac)





      Obviously there is much more, and the conversion rates arent completely accurate but thats the basis. I've tried every possible change I can think of but have no idea how to make this work.


      I am new to Actionscript and dont have any programming experience so any help is much appreciated.





        • 1. Re: New to AS and stuck on IF statements
          alinator11 Level 2

          Hi Dave,


          When you delcare your variables 'var ab:Number = Number(c*0.60)' you are missing a couple of things. First, you need to use the new operator:


          var ab:Number = new Number();


          Also, you need a semicolon to end your statements. Try that.


          I remember when I first started coding... thos semicolons can be a pain.




          • 2. Re: New to AS and stuck on IF statements
            EvyatarBH Level 3

            I recommend to use switch-case statements for this purpose, as you would accomplish what you need much better that way.

            You would need to create nested statements, so pay attention to the curly braces and indentation.

            • 3. Re: New to AS and stuck on IF statements
              alinator11 Level 2

              I agree that the switch statement would make the code easier - and perhaps faster as well. But, I personally never use switch statements in any code I write ever. When I first began programming, an experienced programmer told me that switch statements are frowned upon.


              Here's why (from wikipedia): "When implemented with fall-through as the default path, switch/case statements are a frequent source of bugs among even experienced programmers, given that, in practice, the "break" is almost always the desired path, but not the default behavior of the switch/case construct (at least in C and Java)."


              So, I've always stuck with if, then, else type statements even though they are harder to read and can be more painful to write and have serveral other disadvantages.

              • 4. Re: New to AS and stuck on IF statements
                John Hall Level 4

                I'm sure all of us embrass the mythologies we've learned but I would certainly think that a switch statement would be much cleaner if over three options. Of course, wrapping that in a try/catch construct can be quite useful to have things fail gracefully, plus debug the issues, imho.