4. I Have to Scan It More Than Once?


#1


I don't understand correctly where is the mistake.

The Error is - Oops, try again. Did you scan four of each item?

And I do not understand why the result a lot of decimal places.

Replace this line with your 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;
        }
    }
};

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



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


#2

Hi, @alexd21 ,

Though it may be unclear why it should matter, you can scan the items in the following order to get a better result ...

cashRegister.scan("eggs", 4);
cashRegister.scan("milk", 4);
cashRegister.scan("magazine", 4);
cashRegister.scan("chocolate", 4);

The issue (EDIT (November 4, 2016): might be is) that the rounding of the numbers proceeds differently if the order of the scanning is changed.

(EDIT continued)
Displaying intermediate results is often a good way to gain insight into problems that occur. Following is the scan method, revised to display intermediate totals ...

    scan: function(item, quantity) {
        switch (item) {
        case "eggs": this.add(0.98 * quantity);
        console.log("Bought eggs, total now: " + this.total);
        break;
        case "milk": this.add(1.23 * quantity);
        console.log("Bought milk, total now: " + this.total);
        break;
        case "magazine": this.add(4.99 * quantity);
        console.log("Bought magazines, total now: " + this.total);
        break;
        case "chocolate": this.add(0.45 * quantity);
        console.log("Bought chocolate, total now: " + this.total);
        break;
        }
    }

Now, let's scan the purchases in the expected order ...

cashRegister.scan("eggs", 4);
cashRegister.scan("milk", 4);
cashRegister.scan("magazine", 4);
cashRegister.scan("chocolate", 4);

Output, with Codecademy's testing output omitted ...

Bought eggs, total now: 3.92
Bought milk, total now: 8.84
Bought magazines, total now: 28.8
Bought chocolate, total now: 30.6
Your bill is 30.6

Seems fine. But now let's change the order ...

cashRegister.scan("eggs", 4);
cashRegister.scan("magazine", 4);
cashRegister.scan("milk", 4);
cashRegister.scan("chocolate" , 4);

Output ...

Bought eggs, total now: 3.92
Bought magazines, total now: 23.880000000000003
Bought milk, total now: 28.800000000000004
Bought chocolate, total now: 30.600000000000005
Your bill is 30.600000000000005

Evidently, JavaScript had trouble with the rounding when the cost of the magazines was added to the previous total, which was the cost of the eggs.

Sorry, no magazines to read with your breakfast, this morning. :wink:

(EDIT: November 5, 2016)
This situation is unacceptable. Totaling up the cost of the items correctly should not rely upon our being lucky enough to scan them in the right order. There are a number of ways that we can fix this. One of them is to modify the add method so that it can work with integers instead of floating point numbers while it is doing the math. Here it is ...

add: function(itemCost){
    // Perform integer arithmetic
    tot = this.total * 100;
    ic = itemCost * 100;
    tot += ic;
    tot /= 100;
    // Convert back to floating point number
    this.total = tot;
}

Try this out while varying the order of scanning the items.

Perhaps other users can propose additional solutions to this problem.


#3

Thank you for the detailed answer) In some moments JS behaves very strangely.


#4

Many other programming have similar issues with floating point numbers and financial calculations, and some of them offer various solutions to the problem.

There was a discussion about this issue, regarding JavaScript, at Stack Overflow: Precise Financial Calculation in JavaScript. What Are the Gotchas?.


#5

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