函数,expensiveItem(菜单),应该返回数组中最昂贵的项目,饭菜。在这种情况下,它应该返回“Double Quarter Pounder with Cheese”。相反,它返回“Double Cheeseburger”。我的代码在哪里出错?
var item1 = {
name: "Big Mac",
price: 3.99,
calories: 530
};
var item2 = {
name: "Filet-O-Fish",
price: 3.79,
calories: 390
};
var item3 = {
name: "Double Cheeseburger",
price: 1.59,
calories: 430
};
var item4 = {
name: "Double Quarter Pounder with Cheese",
price: 4.79,
calories: 740
};
var item5 = {
name: "Chicken McNuggets",
price: 4.49,
calories: 190
};
var meals = [item1, item2, item3, item4, item5];
function init() {
var lowestCost = document.getElementById('cheapestItem');
lowestCost.onclick = function() {
cheapestItem(meals);
}
var highestCost = document.getElementById('highestCost');
highestCost.onclick = function() {
expensiveItem(meals);
}
}
window.onload = init;
// return name of cheapest item on menu
function cheapestItem(menu) {
var msg = document.getElementById('msg1'); // is this necessary? Code works fine without it...
var cheapestItem = operation(menu, "less");
msg1.innerHTML = "The cheapest item is " + cheapestItem;
}
// return name of most expensive item on menu
function expensiveItem(menu) {
var msg = document.getElementById('msg2');
var expensiveItem = operation(menu, "greater");
msg2.innerHTML = "The most expensive item is " + expensiveItem;
}
//return result based on given comparator operator
function operation(menu, op) {
var v1 = menu[0].price;
var item;
for (var i = 0; i < menu.length; i++) {
var v2 = menu[i].price;
if ((op == "less" && (v2 < v1)) ||
(op == "greater" && (v2 > v1))) {
result = v2;
item = menu[i].name;
}
}
console.log(item);
return item;
}
<body>
<input type="button" id="cheapestItem" value="Cheapest Item">
</div>
<p id="msg1">message 1</p>
<input type="button" id="highestCost" value="Most Expensive Item">
</div>
<p id="msg2">message 2</p>
</body>
答案 0 :(得分:1)
问题在这里:
if ((op == "less" && (v2 < v1)) || (op == "greater" && (v2 > v1)))
您只是在比较v1
和v2
,但如果条件满足,您还应该v1
分配v2
的值。
以下是循环中的值
3.99 3.99
3.99 3.79
3.99 1.59
3.99 4.79
4.79 4.49
如果您注意到,最后2次操作满足条件。所以你会得到后者。
var item1 = {
name: "Big Mac",
price: 3.99,
calories: 530
};
var item2 = {
name: "Filet-O-Fish",
price: 3.79,
calories: 390
};
var item3 = {
name: "Double Cheeseburger",
price: 1.59,
calories: 430
};
var item4 = {
name: "Double Quarter Pounder with Cheese",
price: 4.79,
calories: 740
};
var item5 = {
name: "Chicken McNuggets",
price: 4.49,
calories: 190
};
var meals = [item1, item2, item3, item4, item5];
function init() {
var lowestCost = document.getElementById('cheapestItem');
lowestCost.onclick = function() {
cheapestItem(meals);
}
var highestCost = document.getElementById('highestCost');
highestCost.onclick = function() {
expensiveItem(meals);
}
}
window.onload = init;
// return name of cheapest item on menu
function cheapestItem(menu) {
var msg = document.getElementById('msg1'); // is this necessary? Code works fine without it...
var cheapestItem = operation(menu, "less");
msg1.innerHTML = "The cheapest item is " + cheapestItem;
}
// return name of most expensive item on menu
function expensiveItem(menu) {
var msg = document.getElementById('msg2');
var expensiveItem = operation(menu, "greater");
msg2.innerHTML = "The most expensive item is " + expensiveItem;
}
//return result based on given comparator operator
function operation(menu, op) {
var v1 = menu[0].price;
var item;
for (var i = 0; i < menu.length; i++) {
var v2 = menu[i].price;
if ((op == "less" && (v2 <= v1)) ||
(op == "greater" && (v2 >= v1))) {
result = v1 = v2;
item = menu[i].name;
}
}
console.log(item);
return item;
}
&#13;
<body>
<input type="button" id="cheapestItem" value="Cheapest Item">
</div>
<p id="msg1">message 1</p>
<input type="button" id="highestCost" value="Most Expensive Item">
</div>
<p id="msg2">message 2</p>
</body>
&#13;
在init
中对数组进行排序。现在你不必循环来获得最小值和最大值。
注意:使用window.load = function
的不良做法,您应该使用window.addEventListener('load', function)
而不是
var item1 = {
name: "Big Mac",
price: 3.99,
calories: 530
};
var item2 = {
name: "Filet-O-Fish",
price: 3.79,
calories: 390
};
var item3 = {
name: "Double Cheeseburger",
price: 1.59,
calories: 430
};
var item4 = {
name: "Double Quarter Pounder with Cheese",
price: 4.79,
calories: 740
};
var item5 = {
name: "Chicken McNuggets",
price: 4.49,
calories: 190
};
var meals = [item1, item2, item3, item4, item5];
var OP_LESS = "less";
var OP_GREATER = "greater";
function init() {
var lowestCost = document.getElementById('cheapestItem');
lowestCost.onclick = function() {
cheapestItem(meals);
}
var highestCost = document.getElementById('highestCost');
highestCost.onclick = function() {
expensiveItem(meals);
}
sortData();
}
function sortData(){
meals.sort(function(a,b){
return a.price > b.price ? 1 : a.price < b.price ? -1 : 0
});
}
window.addEventListener('load', init);
// return name of cheapest item on menu
function cheapestItem(menu) {
var msg = document.getElementById('msg1'); // is this necessary? Code works fine without it...
var cheapestItem = operation(menu, OP_LESS);
msg1.innerHTML = "The cheapest item is " + cheapestItem;
}
// return name of most expensive item on menu
function expensiveItem(menu) {
var msg = document.getElementById('msg2');
var expensiveItem = operation(menu, OP_GREATER);
msg2.innerHTML = "The most expensive item is " + expensiveItem;
}
//return result based on given comparator operator
function operation(menu, op) {
return (op === OP_LESS ? meals[0] : meals[meals.length-1]).name
}
&#13;
<body>
<input type="button" id="cheapestItem" value="Cheapest Item">
</div>
<p id="msg1">message 1</p>
<input type="button" id="highestCost" value="Most Expensive Item">
</div>
<p id="msg2">message 2</p>
</body>
&#13;
答案 1 :(得分:0)
当您找到&#34;更大&#34;时,您总是与初始v1值进行比较。你应该将新值设置为var v1。
if ((op == "less" && (v2 < v1)) ||
(op == "greater" && (v2 > v1))) {
result = v2;
v1=v2; //Set new greater/lower value
item = menu[i].name;
}
答案 2 :(得分:0)
我建议只使用一个变量来测试并返回
function operation(menu, op) {
var price = menu[0].price,
item = menu[0].name;
for (var i = 0; i < menu.length; i++) {
if (
op === "less" && menu[i].price < price ||
op === "greater" && menu[i].price > price
) {
price = menu[i].price;
item = menu[i].name;
}
}
console.log(item);
return item;
}
var item1 = { name: "Big Mac", price: 3.99, calories: 530 };
var item2 = { name: "Filet-O-Fish", price: 3.79, calories: 390 };
var item3 = { name: "Double Cheeseburger", price: 1.59, calories: 430 };
var item4 = { name: "Double Quarter Pounder with Cheese", price: 4.79, calories: 740
};
var item5 = { name: "Chicken McNuggets", price: 4.49, calories: 190 };
var meals = [item1, item2, item3, item4, item5];
function init() {
var lowestCost = document.getElementById('cheapestItem');
lowestCost.onclick = function() {
cheapestItem(meals);
}
var highestCost = document.getElementById('highestCost');
highestCost.onclick = function() {
expensiveItem(meals);
}
}
window.onload = init;
// return name of cheapest item on menu
function cheapestItem(menu) {
var msg = document.getElementById('msg1'); // is this necessary? Code works fine without it...
var cheapestItem = operation(menu, "less");
msg1.innerHTML = "The cheapest item is " + cheapestItem;
}
// return name of most expensive item on menu
function expensiveItem(menu) {
var msg = document.getElementById('msg2');
var expensiveItem = operation(menu, "greater");
msg2.innerHTML = "The most expensive item is " + expensiveItem;
}
//return result based on given comparator operator
function operation(menu, op) {
var price = menu[0].price,
item = menu[0].name;
for (var i = 0; i < menu.length; i++) {
if (op == "less" && menu[i].price < price || op == "greater" && menu[i].price > price) {
price = menu[i].price;
item = menu[i].name;
}
}
console.log(item);
return item;
}
&#13;
<input type="button" id="cheapestItem" value="Cheapest Item">
<p id="msg1">message 1</p>
<input type="button" id="highestCost" value="Most Expensive Item">
<p id="msg2">message 2</p>
&#13;