我刚开始学习Javascript的激动人心的旅程,在我的一课中,我被要求建立一个voidLastTransaction方法来添加到我的虚拟收银机。我已经写出了代码,允许用户取消上次交易的金额。但是,我想知道如何使最后两个事务无效,而不是一个。我想在我想要废除的每笔交易之后调用该函数是一种方法;但我想知道是否有一种更动态的方式来重置存储最后一个事务量的属性,以便它更改为列表上的上一个事务的值,这将成为下一个"最后一个事务&# 34;一旦最后一笔交易被扣除。以下是我目前的代码。提前致谢!
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;
},
voidLastTransaction: function() {
this.total -= this.lastTransactionAmount;
this.lastTransactionAmount = 0;
}
};
cashRegister.scan("eggs", 1);
cashRegister.scan("milk", 1);
cashRegister.scan("magazine", 1);
cashRegister.scan("chocolate", 4);
// I want to void the last 2 transactions
console.log("your bill is" + cashRegister.total);
答案 0 :(得分:1)
我使用数组,更容易获得最后的交易并将项目应用于总数。阵列是顺序的这一事实是跟踪添加的转换的理想候选者。此外,JavaScript数组可以很容易地用作堆栈或队列。
我已将lastTransactionAmount替换为事务:[]
下面是未经测试的,所以可能是马车:
var cashRegister = {
total: 0,
// use array instead,
// for the problem in question it will function as a stack
transactions:[],
add: function(itemCost) {
this.total += itemCost;
this.transactions.push(itemCost); // add each item to array
},
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;
},
voidLastTransaction: function(total) {
// the immediately following single operation, get's the last element
// in the array (stack), which coincides with the last recorded transaction
// and at the same time, it removes the element from the stack!
// So every time this method is called you have one less transaction.
var lastTransactionCost = this.transactions.pop();
this.total -= lastTransactionCost;
}
};
cashRegister.scan("eggs", 1);
cashRegister.scan("milk", 1);
cashRegister.scan("magazine", 1);
cashRegister.scan("chocolate", 4);
console.log("Current outstanding total: " + cashRegister.total);
console.log("About to void the last 3 transactions...");
var numberOfTransactionsToCancel = 3;
while(numberOfTransactionsToCancel--){
cashRegister.voidLastTransaction();
};
答案 1 :(得分:0)
这是我怎么写的。
1)创建一个数据库对象来保存和管理库存。
var inventory = (function() {
var inventory = {
items: {},
exists: function(itemName) {
return typeof this.items[itemName] !== "undefined";
},
add: function(itemName, info) {
if(!this.exists(itemName)) {
this.items[itemName] = info;
}
return this;
},
get: function(itemName) {
if(!this.exists(itemName)) {
throw new Error("item [" + itemName + "] is not in the database.");
}
return this.items[itemName];
}
};
return {
add: inventory.add.bind(inventory),
get: inventory.get.bind(inventory)
};
})();
2)加载/实例化库存。 add()
返回this
,允许链接add
方法。
inventory.add("eggs", {price: 0.98})
.add("milk", {price: 1.23})
.add("magazine", {price: 4.99})
.add("chocolate", {price: 0.45});
3)创建将用作构造函数的Transction
函数/对象,并存储每个事务的详细信息。
function Transaction(itemName, quantity) {
this.itemName = itemName;
this.price = inventory.get(itemName).price;
this.quantity = quantity;
this.total = this.price * this.quantity;
}
4)创建Order
函数/对象,创建该对象以保存和管理每个客户的订单。
function Order() {
this.transactions = [];
}
5)将函数添加到将用于管理订单的Order
原型。
Order.prototype.scan = function(itemName, quantity) {
this.transactions.push(new Transaction(itemName, quantity));
};
Order.prototype.voidLast = function(num) {
num || (num = 1);
var start = this.transactions.length - num;
this.transactions.splice(start, num);
};
Order.prototype.total = function() {
return this.transactions.reduce(function(total, transaction) {
return total + transaction.total;
}, 0);
};
并且,6)全力以赴。
var myOrder = new Order();
myOrder.scan("eggs", 2);
myOrder.scan("milk", 3);
myOrder.scan("eggs", 1);
myOrder.voidLast(2);
console.log(myOrder.total().toFixed(2));
或者,如果在浏览器中执行以下操作:
var byId = document.getElementById.bind(document);
byId("total").innerHTML = "$" + myOrder.total().toFixed(2);
并且7)小提琴:http://jsfiddle.net/L9dqkn1n/。
答案 2 :(得分:0)
试试这个。这对我有用......
var cashRegister = {
total:0,
lastTransactionAmount: 0,
//Dont forget to add your property
add: function(itemCost) {
this.lastTransactionAmount = itemCost;
this.total += 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.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();
cashRegister.scan('chocolate',3);
//Show the total bill
console.log('Your bill is '+cashRegister.total);
`