Another solution for #4 (bug)


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 = {
    add: function(itemCost){ += 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 ';


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.


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.


I would have :wink:

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

Good intuition!