3/7 syntax error

Please help me solve my problem

the error message is SyntaxError: Unexpected token. I have no idea how to solve it. tried to find answer on forum, but no luck

my cod. thanks
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.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 @datarunner52392

  • You have to add “,” (line 6)
  • You have to change “:” to “;” (line 38)

And it works, hold on :wink:

1 Like

thanks, but this is not helped at all. the problem still there

1 Like

You have to add a comma after the curly braces like this:
add: function (itemCost) {
this.total += itemCost;
},
You have to add a comma because 'cashRegister
is an object and ‘add’ is a property of ‘cashRegister’


Hope it works :smile:

2 Likes

The issue you’re referring to is on line 38, not 37. Line 37 does this correctly, whereas line 38 has a : instead of the proper ;.

Hi @kingmaxfield I agree, I changed it thanks :wink:

I struggled with the same problem and this was the answer for me, thanks!

var cashRegister = {
total: 0
//insert the add method here
add: function (itemCost){
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);
    
    }
    return true;
}

};

//Scan 2 eggs and 3 magazines
cashRegister.add(“eggs”);
cashRegister.add(“eggs”);
cashRegister.add(“magazine”);
//Show the total bill
console.log('Your bill is '+cashRegister.total);

Same problem “Sintax Erorr”
I spent 2 hours to find the mistake but I can’t, please help!!!

This is the right code @gigacoder67552
ar cashRegister = {
total: 0,
//insert the add method here
add: function(itemCost){
this.total = itemCost + this.total;
},

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

Thanks I finaly find my mistake

The problem is that you must write this.
switch (item) {
case “eggs”:
this.add(0.98);
break;
and then call it with cashRegister.scan(“eggs”);
if you put egg instead of eggs it crashes e.x.

switch (item) {
case “egg”:
this.add(0.98);
break;

and then calling it with cashRegister.scan(“egg”);

it will fail (they really want to write eggs instead of egg). But the total will be the same

Good to know. What was your mistake? Others might benefit from it

what exactly does the " , " do in the add: function?
Why do i need it? i find this super annoying.