We should be able to use this alternate solution for applying quantity to the cash register scan method!


#1



Latest 16. Building a Cash Register


When using this code, I get the output, "Your bill is 30.6". Which is correct!

However, I still get this error, "Oops, try again. It doesn't seem like your 'quantity' actually works!"

I looked at the hint and it suggested I multiplied the price by the quantity, I got the same output, yet no error.

I feel that using a for loop for quantity should be included in being one of the solutions in this lesson. Although it may be more complicated, I still enjoy the fact that I only have to add one line of code rather than editing 4 different lines.


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

EDIT: Removed typo in code.


#2

Says who? If it was you wouldn't be here flaunting your stuff!


#3

What is ths? Obviously something is amiss.


#4

Whoops, typo! I originally had my for loop, but it kept showing an error, so I changed it to (price)*1. I changed it back to the for loop code I had for this post, forgot to remove the *'s. Should be fixed now!


#5

The for loop (fixed code is above, I had typos in it) gave the exact same output that the hint code gave me ((price)*1 ,on each case in the switch instead of using the for loop) .


#6

Need to see your code if you are sill experiencing problems. If not, then I guess we're done here, eh?


#7

This...

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

Gives the same result as this...

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

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

Yet the first one returns a Code Academy error of, "Oops, try again. It doesn't seem like your 'quantity' actually works!"

Which doesn't make any sense if both return the same correct answer. Restating all of this to avoid the confusion that started with the typo.


#8

This is clearly a lesson checker issue. Just do as asked and don't improvise and I'm sure it will pass.