5 回答
TA贡献1827条经验 获得超7个赞
您可以使用ES6功能并按价格对购物车项目进行排序,然后获得最贵的,例如:
function mostExpensiveItemName(sampleCart) {
const sortBypriceCart = sampleCart.sort((itemA, itemB) => itemB?.price - itemA?.price) // sort descending by price
const expensiveItem = sortBypriceCart[0];
return expensiveItem.itemName
}
你的代码将是:
function mostExpensiveItemName(sampleCart) {
let expensive = 0;
let name;
for(let i = 0; i < sampleCart.length; i++) {
// compares if the next value is higher and replaces it
if(sampleCart[i].price > expensive) {
expensive = sampleCart[i].price; // += is to sum/add values to the same variables, does not apply here
name = sampleCart[i].itemName; // assign the possible final name
}
}
return name;
}
for总是会比例如更快forEach,但sort它是一个很好的选择,它比 更快forEach。
TA贡献1797条经验 获得超6个赞
const sampleCart = [
{
itemName: "Effective Programming Habits",
type: "book",
price: 13.99
},
{
itemName: "Creation 3005",
type: "computer",
price: 299.99
},
{
itemName: "Finding Your Center",
type: "book",
price: 15.00
}
];
function mostExpensiveItemName(sampleCart) {
let expensive = 0;
var name;
for(let i = 0; i < sampleCart.length; i++) {
if(sampleCart[i].price > expensive) {
expensive = sampleCart[i].price;
name = sampleCart[i].itemName;
}
}
return name;
}
console.log(mostExpensiveItemName(sampleCart));
TA贡献1911条经验 获得超7个赞
这并不完全清楚,但似乎您正在寻找最昂贵的物品的名称。
该代码似乎包含不少逻辑错误。
function mostExpensiveItemName(sampleCart) {
let expensive = 0;
var name;
for (let i = 0; i < sampleCart.length; i++) {
if (sampleCart[i].price > 0) {
expensive += sampleCart[i].price;
name += sampleCart[i].itemName;
}
}
return name;
}
您将名称分配给变量,而您应该只返回项目的名称。
function mostExpensiveItemName(sampleCart) {
let mostExpensive = sampleCart[0];
for (let i = 1; i < sampleCart.length; ++i) {
if (sampleCart[i].price > mostExpensive.price) {
expensive = sampleCart[i];
}
}
return mostExpensive.itemName;
}
TA贡献1786条经验 获得超11个赞
最简单的方法是迭代数组 - 将价格与现有的最高价格进行比较,如果其更大则设置一个索引 - 然后使用该索引重新运行数组中的 itemName。
const sampleCart = [
{
itemName: "Effective Programming Habits",
type: "book",
price: 13.99
},
{
itemName: "Creation 3005",
type: "computer",
price: 299.99
},
{
itemName: "Finding Your Center",
type: "book",
price: 15.00
}
];
function mostExpensiveItemName(arr) {
var mostExpensive = 0;
var mostExpensiveIndex = 0;
arr.forEach(function(item, index) {
if(item.price > mostExpensive) {
mostExpensive = item.price;
mostExpensiveIndex = index;
}
})
return sampleCart[mostExpensiveIndex].itemName;
}
console.log(mostExpensiveItemName(sampleCart)); // gives Creation 3005
TA贡献1830条经验 获得超9个赞
const sampleCart = [
{
itemName: "Effective Programming Habits",
type: "book",
price: 13.99
},
{
itemName: "Creation 3005",
type: "computer",
price: 299.99
},
{
itemName: "Finding Your Center",
type: "book",
price: 15.0
}
];
function mostExpensiveItemName(sampleCart) {
// lets find the biggest number
const biggestPrice = Math.max(...sampleCart.map((item) => item.price));
return sampleCart.find((item) => item.price === biggestPrice).itemName;
}
console.log(mostExpensiveItemName(sampleCart));
添加回答
举报