4. I Have to Scan It More Than Once? - calculation error


#1

I checked everything, and I am almost sure them are all grammatically correct.

But I got a result like "Your bill is 30.600000000000005" and it won't let me pass.

What did I do wrong? Help?


Replace this line with your code.


#2

My code:

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("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: Your bill is 30.6

I just changed positions of cashRegister.scan calls. But when I paste your code, got:

Oops, try again. Did you scan four of each item?
Your bill is 30.600000000000005

To pass this lesson try my code. Here I found some explanation


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.