4. I have to scan it more than once?--Passed without actually scanning anything


#1

How does this pass?

It prints out "Your bill is 0"
But shows the successful dialogue
Any thoughts would be most helpful and greatly appreciated.


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;
        }
        for(i=0; i<quantity; i++){
            this.quantity * itemCost;
        }
    }
};

// scan each item 4 times
//Show the total bill
console.log('Your bill is '+cashRegister.total);

ppps: Is there a way to apply syntax highlighting to the code in this post.( I almost rather paste a picture) if so please point the way!


#2

I took the liberty of editing your post, click edit to see how I did it


#3

If you test your code (scan stuff), you'll find it crashes. When submission tests crash, they pass. Amazing design.


#5

So just adding the tab spaces for highlighting? nice!
the code passes because it crashes! whoa! i'm a n00b, I could make this crash often!!
I get it tho, @lonatan Thanks for the help


#6

Indenting everything by 4 spaces works too, can't specify language then though. No, I added triple backticks before and after

```javascript

code goes here

```

But this works as well:

    some
    code

#7

I got something with these:

var cashRegister = {
total:0,
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.scan("chocolate", 4);
cashRegister.scan("milk", 4);
cashRegister.scan("magazine", 4);
cashRegister.scan("eggs", 4);

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


#8

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

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


I have a doubt
#10