7. You Deserve It - Can Someone Explain? - Confused (struggling) Programmer


#1

https://www.codecademy.com/en/courses/close-the-super-makert/0/7?curriculum_id=506324b3a7dffd00020bf661#

I'm passing the course and my code is correct but I don't understand one part:

On line 34 you're supposed to have employee.discountPercent and yet employee doesn't appear anywhere else in the code except in this method.

Why isn't it staffMember.discountPercent ? That is where we defined the discount percentage.

Thank you in advance, guys.

function StaffMember(name,discountPercent){
    this.name = name;
    this.discountPercent = discountPercent;
}

var sally = new StaffMember("Sally",5);
var bob = new StaffMember("Bob",10);

// Create yourself again as 'me' with a staff discount of 20%
var me = new StaffMember("Ron", 20);

var cashRegister = {
    total:0,
    lastTransactionAmount: 0,
    add: function(itemCost){
        this.total += (itemCost || 0);
        this.lastTransactionAmount = itemCost;
    },
    scan: function(item,quantity){
        switch (item){
        case "eggs": this.add(0.98 * quantity); break;
        case "milk": this.add(1.23 * quantity); break;
        case "magazine": this.add(4.99 * quantity); break;
        case "chocolate": this.add(0.45 * quantity); break;
        }
        return true;
    },
    voidLastTransaction : function(){
        this.total -= this.lastTransactionAmount;
        this.lastTransactionAmount = 0;
    },
    // Create a new method applyStaffDiscount here
    applyStaffDiscount: function(employee) {
        this.total -= this.total * (employee.discountPercent / 100);
    }
};

cashRegister.scan('eggs',1);
cashRegister.scan('milk',1);
cashRegister.scan('magazine',3);
// Apply your staff discount by passing the 'me' object 
// to applyStaffDiscount
cashRegister.applyStaffDiscount(me);

// Show the total bill
console.log('Your bill is '+cashRegister.total.toFixed(2));


#2

Because its an argument.. lets see it upclose:

applyStaffDiscount: function(employee) {
        this.total -= this.total * (employee.discountPercent / 100);
}

Then

cashRegister.applyStaffDiscount(me);

Ok lets look through the code.. first lets look at the parameter its giving.. the variable me..Hmm well there must be a variable named me which has the property .discountPercent; right?

var me = new StaffMember("Ron", 20);

Ok thats just the variable lets see what The StaffMember "Class" has to offer us:

function StaffMember(name,discountPercent){
    this.name = name;
    this.discountPercent = discountPercent;
}

There you go! There is discount percent, so me.discountPercent is 20. since we passed in 20 as a parameter when creating the me instance.. now lets go back to the applyDiscount function!

applyStaffDiscount: function(employee) {
        this.total -= this.total * (employee.discountPercent / 100);
    }

Well here according to our code, you must pass in a sally object as an argument... sobasically when you do this:

cashRegister.applyStaffDiscount(me);

Employee is basically the same exact thing as me... so employee.discountPercent IS me.disountPercent; ... I hope you understand! If not please reply saying so!


#3

...So would I be right in saying that the connection is: employee.discountPercent IS me.discountPercent which ALSO IS StaffMember.discountPercent ?


#4

You see... thats where scope comes in handy.. Thats the whole reason of arguments.. basically, the function inside of an object cant see outside of the object.. It only can see variables inside the function, or the obejct it self by calling this.someVarName; Thats why we pass arguments, so we can actually locate the higher level variables, by just putting as an argument.. Let me explain it better by giving a simple example..

var sG = "Hello World"; 
function printInt(var sParam){
 console.log(sParam);
}

You see here.. I cant just say console.log(sG), inside the function, because it dosent know any variables outside the function itself... Thats where parameters come in handy, so i can pass it in!

printInt(sG);

Now it gets sG Variable as a argument, so it actually knows what sG is.If im not clear on this @ronhogen so ill suggest you read a great short article of this: http://www.w3schools.com/js/js_scope.asp . If you still have some troubles please reply sayin so.


#5

Thank you so much for taking the time with this. I'll read through the article.