Skip to main content
October 20, 2013
Answered

Discount Assignment: If/Else Statement

  • October 20, 2013
  • 4 replies
  • 944 views

Its an assignment that I've spent hours on:

http://imageshack.us/a/img834/7124/273e.png

http://imageshack.us/a/img706/4691/fhfd.png

That's how far I've gotten so far, next up is my code:

__________________________________________________________________________________________________________________________

var body_txt:TextField = new TextField();

body_txt.x = 225;

body_txt.y = 300;

body_txt.autoSize = TextFieldAutoSize.LEFT;

var myFormat:TextFormat= new TextFormat();

var yourPurchase:String;

var yourDiscount:String;

var yourPrice:String;

var discount:Number;

var total:Number;

yourPurchase = max_txt.text;

discount: 10.00 / 10%

yourDiscount = discount_txt.text;

yourPrice = price_txt.text;

total = yourPurchase - discount

if(yourDiscount)

{

      trace();

}

else

{

     trace();

}

__________________________________________________________________________________________________________________________

As you can see, I have no clue on how to get the discount written in code, I don't know how.

Please explain as best as you can about how to solve this dilemma (in Laymen's Terms if possible). Thank you!

This topic is closed to new replies. Start a new post to keep the conversation going.
Correct answer kglad

use:

______________________________________________________________________ ____________________________________________________

var body_txt:TextField = new TextField();

body_txt.x = 225;

body_txt.y = 300;

body_txt.autoSize = TextFieldAutoSize.LEFT;

var myFormat:TextFormat= new TextFormat();

// i'm not sure what the above is

var yourPurchase:Number;

var yourDiscount:Number;

var discount:Number=.1;

// the code below (up to formatF) needs to execute AFTER the user completes entry into max_txt

yourPurchase = Number(max_txt.text);

if(yourPurchase>10){
yourDiscount=discount*yourPurchase;
} else {
yourDiscount=0;
}

discount_txt.text=formatF(yourDiscount);

price_txt.text = formatF(yourPurchase*(1-discount));

function formatF(n:Number):String{

// now write a function that converts numbers to dollars and cents for display

}

4 replies

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
October 20, 2013

use:

______________________________________________________________________ ____________________________________________________

var body_txt:TextField = new TextField();

body_txt.x = 225;

body_txt.y = 300;

body_txt.autoSize = TextFieldAutoSize.LEFT;

var myFormat:TextFormat= new TextFormat();

// i'm not sure what the above is

var yourPurchase:Number;

var yourDiscount:Number;

var discount:Number=.1;

// the code below (up to formatF) needs to execute AFTER the user completes entry into max_txt

yourPurchase = Number(max_txt.text);

if(yourPurchase>10){
yourDiscount=discount*yourPurchase;
} else {
yourDiscount=0;
}

discount_txt.text=formatF(yourDiscount);

price_txt.text = formatF(yourPurchase*(1-discount));

function formatF(n:Number):String{

// now write a function that converts numbers to dollars and cents for display

}

October 21, 2013

// now write a function that converts numbers to dollars and cents for display

Thank you greatly for the help, I appreciate it and am almost finished this assignment. One last question: I tried to convert numbers to dollars but I am not sure how. I am still a beginner with programming, but I am slowly getting better. May I ask you how I should convert it? I've looked on Google for help.

kglad
Community Expert
Community Expert
October 21, 2013

:

function formatF(n:Number):String{

    var s:String = String(Math.round(100*n)/100);

    if(s.indexOf(".")==-1){

        return "$"+s+".00";

    } else {

        while(s.indexOf(".")>=s.length-2){

            s=s+"0";

        }

    }

    return "$"+s

}