4/7: can't figure out what's wrong with my code


#1

been stuck on this code for hours and still cant pass. I've even copy pasted other people answer and still it's not working, I don't know what's wrong with this code. im getting really frustrated. the code is below:

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;
}
}
};

// 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);


#2

never mind I figured it out.

instead of this:

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;

it should be this:

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;


#3
var cashRegister = {
    total:0,
    add: function(q, itemCost){
        this.total += q*itemCost;
    },
    scan: function(q, item) {
        switch (item) {
        case "eggs": this.add(q, 0.98); break;
        case "milk": this.add(q, 1.23); break;
        case "magazine": this.add(q, 4.99); break;
        case "chocolate": this.add(q, 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);

Your bill is 30.6

It is correct but I get the error "Oops, try again. It doesn't seem like your 'quantity' actually works!"


#4

Am I the only one to do it this way
var cashRegister = {
total:0,
add: function(itemCost){
this.total += itemCost;
},
scan: function(item, amount) {
switch (item) {
case "eggs":
if(amount >= 2) {
this.add(0.98 * amount);
}else{
this.add(0.98);
}
break;
case "milk":
if(amount >= 2) {
this.add(1.23 * amount);
}else{
this.add(1.23);
}
break;
case "magazine":
if(amount >= 2) {
this.add(4.99 * amount);
}else{
this.add(4.99);
}
break;
case "chocolate":
if(amount >= 2) {
this.add(0.45 * amount);
}else{
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);