制作收银机:javascript [英] Making Cash register: javascript

查看:94
本文介绍了制作收银机:javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个收银机,但是当我编程时,我遇到了这个问题:

语法错误:意外的令牌。



这是我的代码:

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:

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!

推荐答案

在第一个对象的属性列表的最后一个元素后放置';'。修复它:

You put ';' after the last element of the property list in first object. Fix it:
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:

var code = "var a = {a:1, b:2;}"; // some code
try {
   eval(code);
} catch(e) {
   alert(e);
}



为了更好地定位异常并修复它,您可以将异常对象的此类属性用作 fileName stack fileName lineNumber (不幸的是,不是在所有浏览器中), name 消息 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error [< a href =https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Errortarget =_ blanktitle =New Window> ^ ]。



正如您所看到的,您还可以输出错误构造函数的名称,这将告诉您如何错误分类: EvalError InternalError ,..., TypeError URIError 。但是,通常它应该显示在属性 name



这可以称为转换词汇错误例外。 :-)

这个技巧将允许您处理您开发的代码,这些代码对编译语言更为典型。请注意,JavaScript不是一种逐行工作的原始纯解释语言。它的解释在一开始就有了初步的汇编阶段。



-SA


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


这篇关于制作收银机:javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆