Get stuck


#1


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


ReferenceError: applyStaffDiscount is not defined
Oops, try again. It looks like you have an error in your code! Check the error message for more details: ReferenceError: applyStaffDiscount is not defined


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("Mike",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 -= employee.discountPercent / 100 * this.total;
    }
    
};

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

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


#2

applyStaffDiscount isn't defined. You have it as a sub function of the class cash register. Try running the function as a method to the class cash register.

cashRegister.applyStaffDiscount(me);

#3

btw did anybody know what's the purpose of this I just noticed it?

"this.total += (itemCost || 0)"

           and this

"this.lastTransactionAmount = 0;"

oh..     and this one

"console.log('Your bill is '+cashRegister.total.toFixed(2));"

I even can't find toFixed variable in the code..


#4

That is something that the JavaScript course added into my code as well. Not sure why or what it does, since it's the || is an or operator, but the code functions with that in it.


#5

btw I return to the primal issue

Giving me the error: "Oops, try again. false"

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("Mike",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 -= employee.discountPercent / 100 * this.total;
    }
    
};

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));

#6

I find it I had to remove the quotes on "me" from this line

'cashRegister.applyStaffDiscount("me");'

and it worked for passing the waypoint

but, I calculated for myself and my result (0.98 + 1.23 + 80/100 * (3 * 4.99) = 14.186) is a little bit different from the output "Your bill is 13.74".


#7

That's because toFixed is not a variable. A quick google shows that.

"toFixed() returns a string representation of numObj that does not use exponential notation and has exactly digits digits after the decimal place. The number is rounded if necessary, and the fractional part is padded with zeros if necessary so that it has the specified length. If numObj is greater than 1e+21, this method simply calls Number.prototype.toString() and returns a string in exponential notation."


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.