3/7 My compiler lost it!


#1

Everything seems to be in place, still the compiler shows the syntax error and is not treating the 'scan' as a function. guys help please...

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 "chocolate"
        this.add(0.45);
        break;

    case "magazine"
        this.add(4.99);
        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

Each case "xxx" should have a colon at the end like this:
case "xxx":
your chocolate and magazine do not.


#3

Nice catch... thanks bro :grinning:


#4

You are welcome! :wink:


#5

There must be additional problems with the code. This exact code (updated with correct colons) does not pass, claiming:

Oops, try again. There was a problem with your syntax.

Also, in the console:

SyntaxError: missing } after property list


#6

You have to put "," after you close your add function


#7

Well as per my observation I believe, we are putting the 'function' and 'switch' operators inside the 'Object', thus both of these operators are considered as an 'Object property' & like any other property these must be separated by a 'comma'. Believe me your problem will definitely get resolved... :grinning:


#8

This worked for me guy, those who might struggle like me, hope you find this code useful:

var cashRegister = {
total: 0,
//insert the add method here
add: function ( itemCost){

  return 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 "chocolate":
    this.add(0.45);
    break;

    case "magazine":
    this.add(4.99);
    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);


#9

Thank you. ur code helped me.