我正在開發一個codecademy.com教程,我們正在構建一個收銀機。我應該制定必要的工具來取消收銀機的最後一筆交易。因此,我加入總計NaN的項目
- 一個屬性稱爲lastTransactionAmount
- 的代碼進一步線到功能「添加」來設置最後的交易金額
- 從總 減去最後一個事務的函數voidLastTransaction量
- 然後我在底部測試的空白最後交易功能(這是我想到爲void四個巧克力
- 然後我試圖添加三個巧克力
當我運行代碼,我得到
您的賬單是NaN
有人能解釋我做錯了嗎?
var cashRegister = {
total:0,
//Dont forget to add your property
lastTransactionAmount: 0, //this is a property I added
add: function(itemCost) {
this.total += itemCost;
this.lastTransactionAmount = this.itemCost; //I added this to save
},
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 // I made this function to void l
voidLastTransaction: function() {
this.total = this.total - this.lastTransactionAmount;
}
};
cashRegister.scan('eggs',1);
cashRegister.scan('milk',1);
cashRegister.scan('magazine',1);
cashRegister.scan('chocolate',4);
//Void the last transaction and then add 3 instead
cashRegister.voidLastTransaction(); //I'm voiding last transaction
cashRegister.scan('chocolate', 3); //trying to add 3 (instead of 4)chocolate
//Show the total bill
console.log('Your bill is '+cashRegister.total);
@sshekhar我不認爲這是問題。 –
@sshekhar:它*在對象中定義。 – Ryan