I Have to Scan It More Than Once?


#1

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

'TypeError: this.add is not a function'

Other examples are directly modifying the switch statement but my code works but I get a 'TypeError' instead of printing the total to console. Can somebody please explain why.

var cashRegister = {
    total:0,
    add: function(itemCost, quantity){
        this.total += itemCost;
    },
    scan: function(item, quantity) {
        this.add = (item * quantity);
        switch (item) {
        case "eggs": this.add(0.98); break;
        case "milk": this.add(1.23); break;
        case "magazine": this.add(4.99); break;
        case "chocolate": this.add(0.45); break;
        }
    }
};

// scan each item 4 times
cashRegister.scan("eggs", 4);
cashRegister.scan("milk", 4);
cashRegister.scan("magazine", 4);
cashRegister.scan("chocolate", 4);



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

#2

this.add is a function, so it should be treated like any other function. You don't need to set some sort of format.
Get rid of this line:

this.add = (item * quantity);

Also, the add() function requires two parameters - the price and the quantity. Currently, you're only passing it the item cost. In each function call of add(), make sure you also pass it the quantity variable.
For example, this:

case "eggs": this.add(0.98); break;

Should become this:

case "eggs": this.add(0.98, quantity); break;

#3

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