I have to scan it more than once?


#1



So I can't figure out what's wrong here. But when I submit this I get this in the console:
Your bill is 30.600000000000005

And the error message reads:
"Oops, try again. Did you scan four of each item?"

I've tried refreshing the page as well as leaving the page and coming back. Anyone know what the issue is?


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

// scan each item 4 times
cashRegister.scan('eggs', 4);
cashRegister.scan('magazine', 4);
cashRegister.scan('milk', 4);
cashRegister.scan('chocolate', 4);


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


#2

So the thing about floats is that they are not exact in all situations.
Try another order: eggs-milk-magazine-chocolate

The submission test should be allowing slight differences, but it's comparing equality between approximations and that.. doesn't make sense and that's why this happens.

You could also round off the values a bit yourself


#3

Another arrangement did the trick. Interesting that they aren't exact. Thanks for the help!


#4

I am facing the same problem and even after changing the sequence of calling i.e arrangement, the problem continues...


#5

for the add method use semicolon not comma