Skip to content

Instantly share code, notes, and snippets.

@ivancorrales
Last active March 7, 2016 19:03
Show Gist options
  • Select an option

  • Save ivancorrales/38a9e56409e055e9b8ae to your computer and use it in GitHub Desktop.

Select an option

Save ivancorrales/38a9e56409e055e9b8ae to your computer and use it in GitHub Desktop.
Re-factor the code in a functional-programming way
var shoppingCartTotalPrice = (function(){
var items = [
{
name:'Domus IPA',
amount:3,
unitPrice:2.5,
},
{
name:'Cibeles',
amount:6,
unitPrice:2.0
},
{
name:'La Victoria',
amount:2,
unitPrice:1.35
}
];
return (
function(){
var totalPrice = 0;
for(var itemIndex=0;itemIndex<items.length;itemIndex++){
var item = items[itemIndex];
totalPrice+= (item.amount*item.unitPrice)
}
return totalPrice;
}()
);
})();
console.log(shoppingCartTotalPrice);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment