Pulling discountPercent from the StaffMember object


#1

Below I've pasted my code for the last exercise in the .js tutorial.

The problem area of code is marked below with a comment. What I'm having trouble doing is pulling the discountPercent value from the StaffMember object in a later code segment. I'm pretty sure I could refactor the code to have discountPercent be a global variable, but I'd like to fix the problem within the constraints set by the IDE.

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

var sally = new StaffMember("Sally", 5);
var bob = new StaffMember("Bob", 10);
var me = new StaffMember("Trent", 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
    
  //HERE IS THE PROBLEM CODE
    applyStaffDiscount: function(employee){
        this.total = this.total * this.discountPercent;
    }
    
    
};

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

Just take the single quotes away when you call applyStaffDiscount:

cashRegister.applyStaffDiscount(me);


#3

Oh, and the place where you've commented problem code should be this:

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

#4

@chipcoder47783,

As your cashRegister.applyStaffDiscount() method
is expecting a so-called argument which has to have an object Value me
and NOT as you are providing a string Value 'me'

the FUNCTION talk

var myFunc = function( param1, param2) {
       //Begin of  anonymous FUNCTION-BODY
       //VARIABLE -myFunc- has an -anonymous function- assigned
       //this -anonymous function- has 2 PARAMETERS param1 and param2
       //param1 and param2 PARAMETERS are used 
       //as -local- VARIABLES throughout the FUNCTION-BODY

      console.log( param1 + " and " + param2 ) ;

      //End of anonymous FUNCTION-BODY
};

If you want to call/execute the anonymous function
you will have to add a pair of parentheses to the variable myFunc
like
myFunc();
As the anonymous function was defined
as having 2 parameters
you have to provide 2 arguments
in our case 2 string VALUES "Alena" and "Lauren"
like
myFunc("Alena","Lauren");

some quotes from the outer-world:

**argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block**

OR

**"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".**


#5

Thank you, that did the trick!