Created
July 1, 2019 21:11
-
-
Save adyngom/7ddba69ea215381699fbafc5c8c82c20 to your computer and use it in GitHub Desktop.
Solution to vending machine challenge - JavaScript Alpharetta June 2019
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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