3/7 syntax error


#1

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


#2

Hi @datarunner52392

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

And it works, hold on :wink:


#3

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


#4

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:


#5

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


#6

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


#7

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


#8

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!!!


#9

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


#10

Thanks I finaly find my mistake


#11

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


#14

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


#15

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