Another solution for #4 (bug)


#1

I took a different approach, and while it may not be so efficient, it does actually work, contrary to the answer I was given saying:

"It doesn't seem like your 'quantity' actually works!"

I used a for loop :smile:

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

#2

The exercises have internal grading mechanisms that read your code for specific things. The case is probably that it didn't find something that would have been there had you not used the for loop.


#3

right... I understand. Most decent coders probably wouldn't have used a for loop there anyways. :smile:

But I guess you also have the mechanism check the final answer to see if whatever weirdness the student coded accomplished the challenge given.


#4

I would have :wink:

It's just that the lesson grader probably doesn't expect it.

Good intuition!