You deserve it. What if I want to add price of bags used


#1



I want to create a method to add the cost of bags used, every three items a bag is needed.
The method I think is written properly though I can't figure out how and where to get total_items!
In scan: we have the items quantity which is what I need but how to put it in a variable?


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("Giulio",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(me){
        this.total-=this.total * (me.discountPercent/100);
        },

----------------- here the method created--------------------
    addBags: function(){
    if(total_items%3===0){
    return(cashRegister.total_items/3)*0.05;
    }
    else{
        return(cashRegister.total_items/3+1)*0.05;
    }
}
-------------------------------------------------------------------    
};

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

---------------------here the method called--------------------
cashRegister.addBags();
-----------------------------------------------------------------

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


#2

Hello @giuliocipone,
it's simplier than it seems:

Since we only got four things you can buy (eggs, milk, magazine, chocolate), you can simply add those together in the switch.

All you have to do:

At the top of cashRegister:


[...]
var cashRegister = {
* total_items: 0,
total: 0,
[...]

and then in the switch add the quantity of each to your variable like this:


[...]
switch (item) {
case "eggs".
this.add(0.98 * quantity);
* total_items += quantity;
break;
[...]

You should change your function a bit aswell - why so complicated?


addBags: function() {
* if(total_items >= 3) {
* var bagCost = 0.05;
* var bagsNeeded = Math.round(total_items / 3);
* cashRegister.total += bagCost * bagsNeeded;
* // no return needed
* }
}

Caution: Didn't test it yet.
Code marked with a (*) is added new.

Hope I could help you anyways. :slight_smile:


#3

Thank you very much, extremly clear reply.
All across what you righlty added shouldn't I refer at total_items using: this.total_items ?

Second thing:
why is point 1* inside the {} of the IF statememtn and not outside?
addBags: function() {
* if(total_items >= 3) {
* var bagCost = 0.05;
* var bagsNeeded = Math.round(total_items / 3);
1* cashRegister.total += bagCost * bagsNeeded;
* // no return needed
* }
}

Thanks again, really much appreciated.


#4

Hey @giuliocipone,

@ #1: Yeah, you're right, should be this.total_items, sorry, my fault. :stuck_out_tongue:
@ #2: What do you mean?
- Why the if-statement is inside the function, or
- why the var's are inside the if-statement?

First case:
Well, you call addBags and it first checks if there are more or equal (>=) three this.total_items
(I just noticed it: cashRegister.total should be changed in this.total obviously.)
If the statement is true, it runs the code inside the if-statement. But I guess I don't need to explain you how if-statements work :stuck_out_tongue:

Second case:
You can call addBags without knowing, if there are more or equal three items. So why should you allocate memory for the var's, if you probably don't need them? I know, that's not much memory... but if you have a bigger code, it's gonna be profitable. :smiley:

Hope I could help you further. If you still need assistance to this, just let me know. :slight_smile:


#5

Thanks man for you fast reply, forgive my delay in getting back to you.
It works! as soon as I added this. to total_items the function recognised them and added the bags cost.
Thanks again.


#6

Glad to read that! You're welcome :slight_smile: