4. I Have to scan it


#1



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


It does not let me go through with this code, however it works for me and this is an easier solution.


var cashRegister = {
    total:0,
    add: function(itemCost){
        this.total += itemCost * this.quantity;
    },
    scan: function(quantity, item) {
        this.quantity = 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(4, "eggs")
cashRegister.scan(4, "milk")
cashRegister.scan(4, "magazine")
cashRegister.scan(4, "chocolate")


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


#2

Hi the easier solution (I THINK) will be...

var cashRegister = {
    total:0,
    add: function(itemCost){
        this.total += 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;
        }
    }
};

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

just add a argument to the scan method the make the operation this.add(0.98 * quantity)


#3

Thanks for the answer, but I don't understand why is it better to repeat "quantity" so many times when the golden rule is not to repeat yourself.:slight_smile:
This worked for me finally, without repeating "quantity":

var cashRegister = {
total:0,
add: function(itemCost){
this.total += itemCost * this.quantity;
},
scan: function(item, quantity) {
this.quantity = 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);


#4

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