Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Im trying to create a cash register, but as I was programming, I came across this problem:
Syntax Error: Unexpected token.

Here is my code:
C#
var cashRegister = {
    total:0,
 
    lastTransactionAmount: 0,

    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() {
        this.lastTransactionAmount -= this.total;
    };

};

cashRegister.scan('eggs',3);
cashRegister.scan('milk',2);
cashRegister.scan('magazine',3);
cashRegister.scan('chocolate',4);

//Void the last transaction and then add 3 instead
cashRegister.voidLastTransaction("chocolate",4);
cashRegister.scan("chocolate",3);
//Show the total bill
console.log('Your bill is '+cashRegister.total);


//-------------------------------------------------------
I can't find the problem, could you please help out? Thank you!
Posted

1 solution

You put ';' after the last element of the property list in first object. Fix it:
JavaScript
var cashRegister = {
    total:0,

    lastTransactionAmount: 0,

    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() {
        this.lastTransactionAmount -= this.total;
    }  // was invalid ';' after '}'

};

The fix is in the line I commented with '// was invalid ';' after '}''.

That's all.

There is one trick to find out errors in lexical level of the code, which won't be normally caught as exception.
Take the text and put it in some string, same, the variable named code:
JavaScript
var code = "var a = {a:1, b:2;}"; // some code
try {
   eval(code);
} catch(e) {
   alert(e);
}

To better locate the exception and fix it, you can use such properties of the exception object as fileName, stack, fileName and lineNumber (unfortunately, not in all browsers), name and message: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error[^].

As you can see, you can also output the name of the error constructor, which would show you how your error is classified: EvalError, InternalError, …, TypeError, URIError. However, normally it should be shown in the property name.

This can be called "converting lexical errors in to exceptions". :-)
This trick will allow you to deal with the code you develop with the convenience more typical to compiled languages. Note that JavaScript is not a primitive pure-interpretive language working line by line. Its interpretation does have rudimentary "compilation" phase at the very beginning.

—SA
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900