Skip to content

Instantly share code, notes, and snippets.

@adyngom
Created July 1, 2019 21:11
Show Gist options
  • Save adyngom/7ddba69ea215381699fbafc5c8c82c20 to your computer and use it in GitHub Desktop.
Save adyngom/7ddba69ea215381699fbafc5c8c82c20 to your computer and use it in GitHub Desktop.
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