Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Solution to vending machine challenge - JavaScript Alpharetta June 2019
const VM = function(inventory) {
var drinks = inventory || null;
if (!drinks) {
throw new Error("No inventory: Cannot add a new VM");
}
const drinksKeys = Object.keys(drinks);
var sale = function(pid) {
if (!drinks[pid]) {
return;
}
if (drinks[pid].stock > 0) {
drinks[pid].stock--;
return `1 ${drinks[pid].name} - Thank you, come again!!`;
} else {
drinks[pid].stock = 0;
return ` ${drinks[pid].name} is out of stock :( Come back tomorrow`;
}
};
var stock = function() {
const total = drinksKeys.reduce((c, n) => c + drinks[n].stock, 0);
return total;
};
return { sale };
};
const drinks = {
1: { name: "Mango Juice", stock: 2 },
2: { name: "Banana Smoothies", stock: 2 },
3: { name: "Guava Mix", stock: 1 },
4: { name: "Citrus Blend", stock: 3 }
};
const vm = VM(drinks);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment