cashRegister 4/7 it works... why can't I move on?


#1

**

here is the task:

**
Modify the scan method such that if we tell it the quantity of each item, it will be able to add the right amount to the total. Since you currently tell scan nothing about quantity, it may be useful to create another parameter.

Scan 4 of each item using your improved scan method. Previously we would have needed to call scan 16 times! Now it is down to 4.

here is my code:

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

};

// scan each item 4 times
cashRegister.scan("eggs",4)
cashRegister.scan("milk",4)
cashRegister.scan("magazine",4)
cashRegister.scan("chocolate",4)
console.log('Your bill is '+cashRegister.total);

**

when I run it, I get:

**

Your bill is 30.6

**

but I get an error:

**

Oops, try again.

It doesn't seem like your 'quantity' actually works!

**

What am I doing wrong?

**


#2

Just edited your code, remember to use the preformatted text button next time :smile:


#3

Your code is fine by the way. It is a problem with the correction test CC use.

For me the code worked by using a different approach to you.
Instead of a while loop I did this this.add(0.98 * quantity). Since I know its the same item each time scan is called. That should hopefully fix it for you :smile:
A while loop was an interesting way of doing it that should work though, the test is just wrong, we are trying to get it fixed.


#4

thank you once I made the change, it worked.