Building a Cash Register pt4 (Needs Debug)


#1


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


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


#2

why did you add a second switch statement? you shouldn't, this is correct:

switch (quantity) {
            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;
        }

except the argument should of switch should be item, then remove the first switch statement


#3

I'm not following you. What's wrong?


#4

you added a second switch statement, something you shouldn't do. I am not sure which merge you would prefer. You have two switch statements:

# switch statement 1
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;
        }
# switch statement 2
        switch (quantity) {
            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;
        }

but you should only have one switch statement.


#5

Oh. Only one switch? How come?
How do I get it to scan it more than once if there isn't another switch?


#6

well, the switch statement is only to determine what item customer is buying

which you do here correctly:

switch (item) // we need to determine what item customer buys
// for this we use switch statement

then we can just multiply the cost with the quantity when calling add method like you do here:

            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;

so in a way, you do the right things. Now you just need to merge them.


#7

Thank you! I get it now.


#8