I've tried everything, STILL DOES NOT WORK


#1


Pardon the code i got a bit pissed thus I just put in 4 immediately, STILL it gives that darned error message. The actual answer I should be getting is "30.6"

What's wrong with the code?


It keeps computing "30.6000000000005"


var cashRegister = {
    total:0,
    add: function(itemCost){
        this.total += itemCost
    },
    scan: function(item) {
        switch (item) {
        case "eggs": this.add(0.98 * 4); break;
        case "milk": this.add(1.23 * 4); break;
        case "magazine": this.add(4.99 * 4); break;
        case "chocolate": this.add(0.45 * 4); break;
        }
    }
};

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

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


#2

Floats are not exact and the submission test is doing the mistake of doing an exact comparison which doesn't make much sense for something that isn't exact.

I'd try rounding the total each time it is added to, or otherwise change order of operations.


#3

OK, about rounding off, how to do that? I didn't learn of such "math" stuff in JavaScript.

And order of operations where?


#4

search engine: javascript round
order of operations, order that you perform operations


#5

I know what order of operations are, i meant where exactly in my code; because there are 5 places in which order of operations matter, and I'd prefer for you to tell me exactly where so that i can go correct myself and learn from the mistake

And thanks for rounding off tip


#6

Ok well, i redid the whole code again, meaning I re-typed everything again.

It worked, finalllyy


#7

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