Pls HELP


#1

what wrong here
var cashRegister = {
total:0,
total:1,
total:2,
total:3,
add: function(itemCost){
this.total = this.total += itemCost;
switch (item) {
case eggs:
x = 0.98
break;
case milk:
x = 1.23
break;
case magazine:
x = 4.99
break;
case chocolate:
x = 0.45
break;
};
};

};

//call the add method for our items
total.add(cashRegister.eggs);
total.add(cashRegister.milk);
total.add(cashRegister.magazine);
total.add(cashRegister.chocolate);
//Show the total bill
console.log('Your bill is '+ cashRegister.itemCost);


#2

First, let's establish the context object, cashRegister...

var cashRegister = {
    //
};

We want a running total, so we can next initialize a variable for that purpose...

var cashRegister = {
    total: 0,
};

Our object needs a process by which we can add purchase amounts to the running total. This will take two methods, one to scan, and one to add. The scan method will take one parameter, a string, the item name. We will use a switch statement for control flow. The add method will take one parameter, a number, the item cost...

var cashRegister = {
    total: 0,
    scan: function (item) {
        switch (item) {
        case "eggs": this.add(0.98); break;
        case "": this.add(); break;
        case "": this.add(); break;
        case "": this.add(); break;
        }
    },
    add: function (itemCost) {
        this.total += itemCost || 0; // always a number or zero
    }
};

I've left some cases empty so you can practice filling in the details. Note that the scan method refers to add as this.add(). That is because add is in the same object context as the scan method. Were we to just call add() we would get a reference error since add is not defined in any other context. this is the execution context of the function, that being cashRegister.

To add an item we would then call the scan method of the cashRegister object, outside of the object context, so we need to name the object:

cashRegister.scan("eggs");
cashRegister.scan("");
cashRegister.scan("");
cashRegister.scan("");

I've left some empty for you to practice on. Assuming everything is in place,

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

Ping this topic if you are still not clear about what is going on in this code.


#3

Thanks :smiley: thiis is the solution