7. You deserve it


#1
var bob = new StaffMember("Bob",10);

// Create yourself again as 'me' with a staff discount of 20%
var me = new StaffMember("Fredy",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 / 100 * this.discountPrecent;
    }
};

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

So... cant pass witht his code. Can some1 explain whats wrong with it? And how could I make it work?


#2

@mrbumbum Hey!
On line 35, make sure to remember to use the -= operator instead of the subtracting sign! If you use the subtracting sign, you wont initialize it or actually subtract it the total.
In addition to that, it's a function so you'll need to pass in "employee.discountPercent"
Hope this helps!


#3

The line 35 it didnt want to accept at all... had to use another way to remove 20% from total. = 3=
But thanks for the help dude! ^.^


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

#5

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("me",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

Hi all, I used this code, which is essentially the same as above, however in the end I was struggling because of the " ":

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("Julien",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;
    },
    applyStaffDiscount: function(employee) {
        this.total = this.total * (1 - employee.discountPercent / 100);
    }
};

cashRegister.scan('eggs',1);
cashRegister.scan('milk',1);
cashRegister.scan('magazine',3);

cashRegister.applyStaffDiscount(me);

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

In my first version (which did not compile) there was only one difference. Why does this have to be?

cashRegister.applyStaffDiscount(me);

While this line does not compile.

cashRegister.applyStaffDiscount("me");

Why do I now need to remove the " "?


#7

Who can explaine what the ..."toFixed(2)" is!?


#8

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("SS", 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
applyStaffDisscount: function(employee) {
if (typeof me === "object")
{
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.applyStaffDisscount(me);

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

/* ISSUE */
Get error: false.
My result is "Your bill is 13.74"

Could anyone help me please? Thanks.


#9

using "me" is using the name, rather than the variable. You should be applying the staff discount to the entire employee (with the name "me" and the discount value of 20) rather than applying it to the name "me". it's slightly confusing but if you work backwards and try to plug and chug a little bit I think you'll get it.

remember that

cashRegister.applyStaffDiscount(me);

is using accessing the function from above (apply staff discount):

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

"me".discountPercent is not valid(string); me.discountPercent is.

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

#10

Who can explaine what the ..."toFixed(2)" is!?

Because that way only 02 decimal numbers will be showed in the cash register total.


#11

whats wrong with this code, im getting error like Oops try again false & in output im getting your bill is NaN

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


#12

"toFixed() converts a number to a string with a specified number of digits after the decimal point" (Flanagan, 48). In other words, it tells the JavaScript interpreter that it is only allowed to interpret the value of that number to a certain number of places past the decimal point. If the number goes past the place value set, the interpreter will round the number to that place.

Example: var x = 3.141592654
console.log(x.toFixed(2)); -----> Prints out 3.14

Source: Flanagan, David. JavaScript: The Definitive Guide. 6th ed. Sebastopol, CA: O'Reilly Media, 2011. Print.


#13

check this against the code you have above:

Capitalisation should solve it :slight_smile:


#14

Ah I see. // Create a new method applyStaffDiscount here
applyStaffDiscount: function(employee) {
this.total -= this.total * (employee.discountpercent / 100);
}
};
this code is the problem right here. They want you to change employee. Put a different name.
Solution:

// Create a new method applyStaffDiscount here
applyStaffDiscount: function(Shrek) {
this.total -= this.total * (Shrek.discountpercent / 100);
}
};


#15

Why does the cashRegister.add method have the this.total += itemCost or 0. E.g.,


add: function(itemCost){
this.total += (itemCost || 0);
this.lastTransactionAmount = itemCost;
},


#16

All it does it round the answer to 2 decimal places. so for example if the answer is 16.2345. toFixed(2) will round the answer to 2 decimal places as 16.23.

We are using toFixed because usually when calculating percentages as in this exercise it will give results to a lo off decimal places which is not necessary to practical purposes.


#17

does anyone know why in add method, need use
this.total += (itemCost || 0)

but not
this.total += itemCost?

I tried both ways can successfully run out result.