【发布时间】:2026-01-27 19:20:03
【问题描述】:
我如何以最有效的方式解决这个问题?我们应该使用 .reduce() 和这些方法,还是应该使用经典的 for in 循环来遍历 allStore 中的键并使用配方进行计算?
var soup = { //recipe
potato: 3,
onion: 1,
corn: 5
};
var edoka = {
cheese: 8,
corn: 3,
meat: 6,
onion: 4,
pea: 1,
oregano: 7,
potato: 5,
tomato: 6
};
var were = {
cheese: 6,
corn: 2,
meat: 9,
onion: 5,
pea: 2,
oregano: 6,
potato: 3,
tomato: 3
};
var brutto = {
cheese: 6,
corn: 2,
meat: 9,
onion: 5,
pea: 2,
oregano: 8,
potato: 3,
tomato: 4
};
var allStores = { // this is an example of a "storeCollection"
Brutto: brutto,
Edoka: edoka,
Were: were,
};
function cheapestStoreForRecipe(recipe, storeCollection){
// make it return the key for the store in storeCollection
// that has the cheapest total cost for recipe. Feel free
// to use costOfRecipe inside this function!
}
【问题讨论】:
-
我认为这是你的功课,你刚刚给了我们属于它的代码。您自己的代码在哪里以及您尝试解决的问题?
标签: javascript arrays function object