Skip to content

Instantly share code, notes, and snippets.

@daniellealexis
Last active January 28, 2018 06:58
Show Gist options
  • Save daniellealexis/a6bec970258c94c2841c356ec1f3e36d to your computer and use it in GitHub Desktop.
Save daniellealexis/a6bec970258c94c2841c356ec1f3e36d to your computer and use it in GitHub Desktop.
const coffeeShoppe = {};
coffeeShoppe.coffee = 5.25;
coffeeShoppe.latte = 4.25;
coffeeShoppe.cheesecake = 4.00;
/**
* Get the price of the passed-in product name
* @param {string} productName
* @return {number|undefined}
*/
coffeeShoppe.getPrice = function(productName) {
return (typeof productName === 'string') ?
this[productName] :
undefined;
};
/**
* Get the total price of a list of products
* @param {string[]} productList
* @return {number}
*/
coffeeShoppe.calculateTotalPrice = function(productList) {
let total = 0;
if (!Array.isArray(productList)) {
return total;
}
const productCount = productList.length;
for (let i = 0; i < productCount; i++) {
const price = this.getPrice(productList[i]);
if (typeof price === 'number') {
total += price;
}
}
return total;
};
coffeeShoppe.calculateTotalPrice(['bagel', 'coffee', 'latte']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment