Short Term Memory//Bleep Bleep help?


#1



https://www.codecademy.com/courses/close-the-super-makert/0/3?curriculum_id=506324b3a7dffd00020bf661
and
https://www.codecademy.com/en/courses/close-the-super-makert/0/5?curriculum_id=506324b3a7dffd00020bf661


Sorry to stuff so much code into this one question but here: this keeps on giving me the error message "SyntaxError: Unexpected Token" but not specifying what the token in question IS.


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); 
            break;
        
        case "milk": 
            this.add(1.23); 
            break;
        
        //Add other 2 items here
        case "magazine":
            this.add(4.99);
            break;
        
        case "chocolate":
            this.add(0.45);
            break;
        
        return true;
    }
};

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

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

This one's a bit more of a general question. It returns "ReferenceError: lastTransactionAmount is not defined", although I did define it. I suspect this has something to do with private vs. public, but I'm not sure how I would fix it.

var cashRegister = {
    total:0,
    //Dont forget to add your property
    add: function(itemCost) {
        this.total +=  itemCost;
        var lastTransactionAmount = itemCost
    },
    scan: function(item,quantity) {
        switch (item) {
        case "eggs": this.add(0.98 * quantity); break;
        case "milk": this.add(1.23 * quantity); break;
        case "magazine": this.add(4.99 * quantity); break;
        case "chocolate": this.add(0.45 * quantity); break;
        }
        return true;
    },
    //Add the voidLastTransaction Method here
    voidLastTransaction: function() {
        this.total = this.total-lastTransactionAmount;
        console.log(this.lastTransactionAmount)
    }
    
};

cashRegister.scan('eggs',1);
cashRegister.scan('milk',1);
cashRegister.scan('magazine',1);
cashRegister.scan('chocolate',4);

//Void the last transaction and then add 3 instead
cashRegister.voidLastTransaction()

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


#2

The first one:
1- you didn't put a curly bracket before return true;

2- instead of those lines:

write those lines:

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

#3

The second one:
1- delete var and write this. instead

2- write this. before lastTransactionAmount

3- After this line:

write this line:
cashRegister.scan("chocolate",3);


#4

I did these things, but it still tells me that lastTransactionAmount is undefined.


#5

Could you please write your current code here ?


#6

'''
var cashRegister = {
total:0,
//Dont forget to add your property
add: function(itemCost) {
this.total += itemCost;
var lastTransactionAmount = itemCost
},
scan: function(item,quantity) {
switch (item) {
case "eggs": this.add(0.98 * quantity); break;
case "milk": this.add(1.23 * quantity); break;
case "magazine": this.add(4.99 * quantity); break;
case "chocolate": this.add(0.45 * quantity); break;
}
return true;
},
//Add the voidLastTransaction Method here
voidLastTransaction: function() {
this.total = this.total-lastTransactionAmount;
console.log(this.lastTransactionAmount)

}

};

cashRegister.scan('eggs',1);
cashRegister.scan('milk',1);
cashRegister.scan('magazine',1);
cashRegister.scan('chocolate',4);

//Void the last transaction and then add 3 instead
cashRegister.voidLastTransaction()
cashRegister.scan("chocolate",3);

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

'''

(the formatting is malfunctioning here, but hopefully this isn't too difficult to read).


#7

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