I have to scan it more than once?


Continuing the discussion from 4. I Have to Scan It More Than Once?:

But why,, isn't quantity supposed to be defined as a paramenter of the switch function?


no, it is not.

the switch statement is used to detect which item the customer wants to buy, once you determine which item the customer you want to buy, you can just multiply the item with the quantity. (which we can access thanks to function parameter)

Why would we want to provide quantity as parameter for the switch statement?

it would be very illogical to check for all possible quantity's using a switch statement, this will make the code base huge.



I have tried submiting my code multiple times with the quantity parameter defined on the scan function, for some reason it completely do not understand it fails to run; gives me an error asking whether I scanned four of each item, which as you can see from the code below I have, so I decided to define it under a the switch function; didn't give me the answer I was lookning for but it ran.

var cashRegister = {
    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.add('eggs', 4);
cashRegister.add('milk', 4);
cashRegister.add('magazine', 4);
cashRegister.add('chocolate', 4);

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


from the instructions:
Scan 4 of each item using your improved scan method

lets see:

cashRegister.add('eggs', 4);

you seem to still use the add method, you should upgrade to the scan method:

cashRegister.scan('eggs', 4);


Finally works, I get it now.
I chose not to use the cashRegister.scan(...) cause I wasnt sure how the scan would add them together as its being used to store the items. I had not however gone through the code well to see this.add would take care of the addition. Thanks alot, it was starting to give me a headache for a sec there.


yep, you seem to understand it perfectly :slight_smile: