Print messages seen in console when not expected 3. Short term memory


#1

I'm still trying to get my head around how this all works. Can someone help me? I get the code to pass but I still think I have something wrong or extra.

I added a console.log confirmation and print out of the subtotal as I went to check if the items were getting added. When I run the code the correct 2x eggs and 3x magazines get added but I also get an extra set of eggs and an extra magazine. Why?

The console returns:

Eggs added, subtotal: 0.98
Eggs added, subtotal: 1.96
Magazine added, subtotal: 6.95
Magazine added, subtotal: 11.940000000000001
Magazine added, subtotal: 16.93
Your bill is 16.93
Eggs added, subtotal: 0.98
Magazine added, subtotal: 7.2


var cashRegister = {
    total: 0,
//insert the add method here    
    add: function (itemCost) {
        this.total += itemCost  
    },
    
    scan: function (item) {
        switch (item) { 
        case "eggs": 
            this.add(0.98); 
            console.log("Eggs added, subtotal: " + this.total);
            break;
        
        case "milk": 
            this.add(1.23); 
            break;
        
        //Add other 2 items here
        case "magazine":
            this.add(4.99);
            console.log("Magazine added, subtotal: " + this.total);
            break;
        
        case "chocolate":
            this.add(0.45);
            break;
        }
        return true;
    }
};

//Scan 2 eggs and 3 magazines
cashRegister.scan("eggs");
cashRegister.scan("eggs");
cashRegister.scan("magazine");
cashRegister.scan("magazine");
cashRegister.scan("magazine");


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


#2

The SCT (Submission Correctness Test) is calling your code to see if it works perfectly. Thus, when it calls your cashRegister.scan, after your code has finished execution, there are two extra printed lines. If you'll add console.log statements to all the case blocks, like this:

scan: function (item) {
        switch (item) { 
        case "eggs": 
            this.add(0.98); 
            console.log("Eggs added, subtotal: " + this.total);
            break;
        
        case "milk": 
            this.add(1.23); 
            console.log("Milk added, subtotal: " + this.total);
            break;
        
        case "magazine":
            this.add(4.99);
            console.log("Magazine added, subtotal: " + this.total);
            break;
        
        case "chocolate":
            this.add(0.45);
            console.log("Chocolate added, subtotal: " + this.total);
            break;

you'll get this output:

Eggs added, subtotal: 0.98
Eggs added, subtotal: 1.96
Magazine added, subtotal: 6.95
Magazine added, subtotal: 11.940000000000001
Magazine added, subtotal: 16.93
Your bill is 16.93
Eggs added, subtotal: 0.98
Milk added, subtotal: 2.21
Magazine added, subtotal: 7.2
Chocolate added, subtotal: 7.65

ie., four extra printed lines, due to the SCT.


#3

Thanks for looking at it. Glad I wasn't doing anything crazy!


Short term memory --- "Oopss" message counts too many scans