Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
module.exports = {
create: function () {
var display = [
{ name: "chips", price: 0.75, quantity: 5 },
{ name: "soda", price: 1.50, quantity: 0 },
{ name: "candy", price: 1.00, quantity: 5 }
];
var vendingMachine = {
"restock": function (itemIndex) {
var selectedItem = display[itemIndex];
// Validations
if (!selectedItem) {
return "Invalid selected item";
}
selectedItem.quantity = 5;
return selectedItem.name + " was restocked";
},
"purchase": function (paymentAmount, itemIndex) {
var selectedItem = display[itemIndex];
// Validations
if (!selectedItem) {
return "Invalid selected item";
}
if (selectedItem.price > paymentAmount) {
return "Insufficient funds";
}
if (selectedItem.quantity <= 0) {
return "Out of stock";
}
// Calculate change owed
var changeOwed = paymentAmount - selectedItem.price;
// Dispense product
selectedItem.quantity--;
return "Dispensed " + selectedItem.name + ". Change owed: " + changeOwed;
}
};
return vendingMachine;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment