5/7 I have no idea what's wrong


#1

Whenever I run this code I get "SyntaxError: Expected '}' ". However, I can't find what's wrong. Any help would be appreciated.

var cashRegister = {
total:0,
lastTransactionAmount: 0
//Dont forget to add your property
add: function(itemCost) {
this.total += itemCost;
this.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() {
total -= 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);

Thanks!!!


#2

i have a problem there too but,,,

i wanna say that your lastTransactionAmount is not accurate
coz u made it equal to itemCost

but lastTransactionAmount it should be equal to last scan as it is not always will be one item for last transaction

meaning uit should be item =Cost * quantity
but when i tried that i got: (quantity is not defined )

lets keep trying
:smiley:


#3

correction

itemCost * quantity


#4

i definitely could be wrong
dont rely on my post


#5

Your code has a number of very basic syntax errors. I should'nt have to point that out to you if you have'nt already solved it.
One of the disciplines you should learn as a beginning programmer is to review your code structure and grammer.
Dump it to a simple text editor if you have to, and format the tabs so you can see the structure easily


#6

corepro16878

i did it :smile:

and by the way your error is happening because of

-- lastTransactionAmount: 0 // should end with comma

-- total -= lastTransactionAmount // should be this.total


#7

ok

i got it all for u,,

first: u don`t need this line lastTransactionAmount: 0
second: inside add method type lastTransactionAmount = itemcost; // not this .lastTransactionAmount
third: inside voidLastTransaction method type this.total // not just total

try these corrections and see


#8

the explanation for that is

u were making a property (lastTransactionAmount)
then using it inside another property

rather u should make the (lastTransactionAmount) as a variable inside the add method.

the other mistake about total and this.total is clear i beleive , we refer to a property by giving (this.property)


#9

now ur code will work

i tried it myself
:smile:


#10

I went about it a completely different way. I hope this helps. :smile_cat:

var cashRegister = {
    total:0,
    add: function(itemCost){
        this.total += itemCost;   
    }
};
var groceries = 
    {eggs: .98,
    milk: 1.23,
    magazine: 4.99,
    chocolate: .45
    };

//call the add method for our items
for (var x in groceries)
    {cashRegister.add(groceries[x]);
    };

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

#11

But you don't address quantities at all here. Your code just runs through the entire list once. What if we want to add 4 or 5 of an object and then remove some?

For voiding the last transaction amount, all that's needed is to save the initial total in a separate variable before it had the most recent item added.