Skip to content

Instantly share code, notes, and snippets.

@ms2sato
Created June 5, 2013 11:44
Show Gist options
  • Save ms2sato/5713319 to your computer and use it in GitHub Desktop.
Save ms2sato/5713319 to your computer and use it in GitHub Desktop.
var ITEMS = [
{price: 250},
{price: 200},
{price: 150}
];
var SET_PRICE = 550;
function minCountItem(items){
var ret = items[0]
for(var i = 1; i < items.length; ++i){
ret = Math.min(ret, items[i]);
}
return ret;
}
//console.log(minCountItem([3,3,4]));
/**
* @params items [ハンバーガーの個数、ポテトの個数、ドリンクの個数]
*/
function buy(items){
var minCount = minCountItem(items);
var totalPrice = SET_PRICE * minCount;
var itemCount;
for(var i = 0; i < ITEMS.length; ++i){
itemCount = items[i] - minCount;
totalPrice += ITEMS[i].price * itemCount;
}
return totalPrice;
}
console.log(buy([1,1,1]));
console.log(buy([2,1,1]));
console.log(buy([3,3,1]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment