Last active
January 28, 2018 06:58
-
-
Save daniellealexis/a6bec970258c94c2841c356ec1f3e36d to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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