<In what way does your code behave incorrectly? Include ALL error messages.>
SyntaxError: Unexpected identifier
```
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);
case "chocolate"
this.add(0.45);
}
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);
im getting this error: SyntaxError: Unexpected token
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);
case "chocolate"
this.add(0.45);
}
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);
rodeman: i finally understan your āadd commaā solution. Could you please explain me why? Usually when you finish a function you add the semicolon };
Otherwise at the end of the next method, we donāt finish }, nor }; we just end with }
This is kind off confusing, could you please help me?
I believe need a comma after the add function because we are working inside an object and each item inside an object should be separated by a comma. Not entirely sure though, but that would explain it.
Does anyone have the correct answer? I have ben trying to work it out for a long time and havenāt succeeded, help!!! why is this wrong?
var cashRegister = {
this.total: 0,
this.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;
case "magazine":
this.add(4.99);
break;
case "chocolate":
this.add(0.45);
break
}
return true;
}
};
cashRegister.scan(āeggsā);
cashRegister.scan(āeggsā);
cashRegister.scan(āmagazineā);
cashRegister.scan(āmagazineā);
cashRegister.scan(āmagazineā);
console.log('Your bill is '+cashRegister.total);
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;
case "magazine":
this.add(4.99);
break;
case "chocolate":
this.add(0.45);
break;
//Add other 2 items here
}
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);
this code works!!!
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;
}
};
//Scan 2 eggs and 3 magazines
cashRegister.add(0.98);
cashRegister.add(0.98);
cashRegister.add(4.99);
cashRegister.add(4.99);
cashRegister.add(4.99);
//Show the total bill
console.log('Your bill is '+cashRegister.total);
Itās the tiny comma! right after the add function, put a comma and it should run. Thatās what the problem was for me anyway. The below code worked for me
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;
}
//insert the add method here
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;
}
};
//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);
Hi Mathewvirgen, you are doing a calculation on the item and then sending it to the function. So, the process would attempt to multiple 2 into the word eggs and then send over the output to Scan function.
This defeats the whole point of the exercise. It even says it in the question the point is to demonstrate that you can use the name of the shopping item instead of the price.
I got confused by this too - hadnāt got my head round the fact that scan was still part of the object (and so the add function should have a comma after it), thanks for the tip!