I Have to Scan It More Than Once? (with a For loop)



This code when run produces the correct total (30.6). I think the background checker doesn't allow this method (I get an error message pop-up). Point is: I should be able to use a For loop here to iterate over the case statement [quantity] times -- this solution should be allowed and for some reason it isn't. Any mods here that can look into this?

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

// 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);


This is answered here.

Floating point math makes different approaches to the same problem potentially different and the checker has it's own way. This way is safe to use since the numbers are simple, but can produce different results with less representable floats (1/3 for instance).


You don't need for loop!
(I mean it can be done without loop,switch will just work fine.)


I was more concerned that a right answer wasn't being acknowledged, regardless of whether it's necessary or not. It might give people the wrong ideas about how they should be coding if they're given unnecessary errors.


redundant code always creates bugs ,though I agree with you.


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