4/7 with "For" what's wrong?


#1

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

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


#2

There is no need of any for loop there.
The scan function should be like this :

scan: function(item,count) {
        switch (item) {
        case "eggs": this.add(0.98 * count); break;
        case "milk": this.add(1.23 * count); break;
        case "magazine": this.add(4.99 * count); break;
        case "chocolate": this.add(0.45 * count); break;
        }
    };

Please accept it as solution if it is working now.
There is another request:
Please like my post in adding C++ course under the features category
http://discuss.codecademy.com/t/c-course/8658


#3

I know how to do it using only multiplication. But my interest is to implement using "For"


#4

Hi gigasolver46414.

My solution works, but don't return a sum of the items. Can you please tell me why?

var cashRegister = {
total:0,
add: function(itemCost){
this.total += (itemCost * quantity);
},

scan: function(item, count) {
    switch (item) {
    case "eggs": this.add(0.98 * count); break;
    case "milk": this.add(1.23 * count); break;
    case "magazine": this.add(4.99 * count); break;
    case "chocolate": this.add(0.45 * count); 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);

Result

ReferenceError: quantity is not defined