Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

How do you a add a discount variable to a script?

I can't see where I've gone wrong - I want to create a 10% discount if the bill is over 10. I have pretty much everything set in place. Yet, my billWithDiscount variable keeps coming back '0'.

I don't know If I haven't written the discount variable correctly...

Any help would be amazing, thanks

function Order(name, price) {
    this.name = name;
    this.price = price;
}

let latte = new Order('Latte', 2.55);
let flat = new Order('Flat White', 3.75);
let cappuccino = new Order('Cappuccino', 2.10);
let americano = new Order('Americano', 4.10);

let bill = 0;

let discount = (bill * 10) / 100;

let billWithDiscount = (bill - discount);

let myOrder = (drink) => {
    console.log('You ordered a ' + drink.name + '.' + ' This drink costs ' + '£' + drink.price.toFixed(2));
    bill += drink.price;
}

myOrder(flat);
myOrder(latte);
myOrder(cappuccino);
myOrder(americano);

if(bill >= 10) {
console.log('You have spent over £10 - you get a 10% discount');
console.log('Your total bill is ' + '£' + billWithDiscount);
}

2 Answers

Try running this and you'll see the problem.

function Order(name, price) {
    this.name = name;
    this.price = price;
}

let latte = new Order('Latte', 2.55);
let flat = new Order('Flat White', 3.75);
let cappuccino = new Order('Cappuccino', 2.10);
let americano = new Order('Americano', 4.10);

let bill = 0;

let discount = (bill * 10) / 100;

let billWithDiscount = (bill - discount);

let myOrder = (drink) => {
    console.log('You ordered a ' + drink.name + '.' + ' This drink costs ' + '£' + drink.price.toFixed(2));
    bill += drink.price;
    console.log("New Bill is:" + bill);
    console.log("Bill with discount is " + billWithDiscount)
}

myOrder(flat);
myOrder(latte);
myOrder(cappuccino);
myOrder(americano);

if(bill >= 10) {
console.log('You have spent over £10 - you get a 10% discount');
console.log('Your total bill is ' + '£' + billWithDiscount);
}

You are not recalculating the billWithDiscount.

here is the answer...

function Order(name, price) {
    this.name = name;
    this.price = price;
}

let latte = new Order('Latte', 2.55);
let flat = new Order('Flat White', 3.75);
let cappuccino = new Order('Cappuccino', 2.10);
let americano = new Order('Americano', 4.10);

let bill = 0;

let discount = (bill * 10) / 100;

let billWithDiscount = (bill - discount);

let myOrder = (drink) => {
    console.log('You ordered a ' + drink.name + '.' + ' This drink costs ' + '£' + drink.price.toFixed(2));
    bill += drink.price;
        discount = (bill * 10) / 100;
        billWithDiscount = (bill - discount);    
}

myOrder(flat);
myOrder(latte);
myOrder(cappuccino);
myOrder(americano);

if(bill >= 10) {
console.log('You have spent over £10 - you get a 10% discount');
console.log('Your total bill is ' + '£' + billWithDiscount);
}

why do you have to define the discount and billWithDiscount variables inside that function, though? I declare them in the global scope. Surely that means with each new object made, the price of that object gets added to the variable in the global scope? I can then access that variable with an 'if' statement?

Thanks

Global variables are available to all child scope.

Any changes to these variables in child scope is reflected back into global scope and hence, this change is available for all child scope as well.

If defined in the function, the new variable formed will be available to the child scope(function) only.

I haven't defined it inside the function, just used(updated it) it inside the function.

The compiler reads a line of code and moves forward. So its not going to come back to the lines...

let discount = (bill * 10) / 100;


let billWithDiscount = (bill - discount);

Whereas everytime the function is called, the control is jumped to the place where the function-code is written and executes all the task specified 'in' the function and control returns back to where the function was called from. Here we do need to tell the control that "update the 'discount' and 'billWithDiscount' " variables every time there is new item added into the field.

If not clear, please do mention so.

Right! So because the script is jumping back and forth between the where the function is created, and where the function is called, that means the the 'discount' and 'billWithDiscount' variables never get revisited? This is because the complier reads one line and moves down, and only jumps back up if it needs to reference something like a function (if a function has been called, of course) - am I understanding this correctly?

Thanks

You are spot on.

To update the variable's content we reminded the control to again do the required operations in the function and hence, the variable stores the updated value.

Enjoy!

Thank you so much! You really helped me understand this concept