7/7 syntay error:unexpected identifier


#1

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("Martin",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
function applyStaffDiscount (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

anyone knows what i'm doing wrong?


#3

You missed a colon between function and applyStaffDiscount-

function applyStaffDiscount (employee) {

I think that's it, but there might be more. Anyone else see anything?


#4

function applyStaffDiscount (employee) { has been wrongly written. Since it is part of an object literal definition, it should be written as:

applyStaffDiscount: function (employee) {

#5

This will work :grinning:

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("Kshitij", 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));

#6

Can you explain what this means? I don't understand it at all... :sweat:


#7

Maybe I can explain it for you:

this.total -= this.total (emplyee...) means, that
this.total = this.total - (employee discount) .. Yes that seems weird...

So the total price is -> this.total.
This are the 20percent from your employee discount -> this.total * (employee.discount / 100)
Because the exercise says that the "me" discount are 20 percent (-> employee.discount), we can write this formel:
this.total * (20/100) ... or just this.total * (0.2)

In the fact that each discount is depending on the price you have to pay in total,
we have to substract the employee.discount from this.total.

The -= operator says that the computer (or anyone else) have to put the result of this.total * 0.2 in the really this.total, which stands at the beginning of the calculation and is the....how can I say that..... total price, the customer/ employee has finally to pay.

Hope that will help you!


#8

It means that,

this.total = this.total - this.total * (employee.discountPercent / 100);

I hope you understood


#9

Thank you for your explanation! It makes sense to me now! Greatly Appreciate the help! :smile:


#10

I know its there already but why is is cashRegister.total.toFixed(2));


#11

.toFixed(2) will round the answer to two decimal places. So, if you get a really long decimal result after doing math like 2/3 then the user won't get troubled taking out change (because of the too many decimal places) :wink:


#12

Or if you want to round up you could do Math.ceil or to round down Math.floor