最贵的物品的 >> NAME << 函数

我已经学习 Javascript 几个星期了,这就是我最近两天陷入困境的地方。我很确定这是最明显的代码行,但我无法将我最昂贵的商品的编号与其名称相匹配。


这就是我所拥有的:


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;

}


这些是数据:


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

  }

]


开满天机
浏览 164回答 5
5回答

慕仙森

您可以使用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。

FFIVE

const sampleCart = [&nbsp; {&nbsp; &nbsp; itemName: "Effective Programming Habits",&nbsp; &nbsp; type: "book",&nbsp; &nbsp; price: 13.99&nbsp; },&nbsp; {&nbsp; &nbsp; itemName: "Creation 3005",&nbsp; &nbsp; type: "computer",&nbsp; &nbsp; price: 299.99&nbsp; },&nbsp; {&nbsp; &nbsp; itemName: "Finding Your Center",&nbsp; &nbsp; type: "book",&nbsp; &nbsp; price: 15.00&nbsp; }];function mostExpensiveItemName(sampleCart) {&nbsp; let expensive = 0;&nbsp; var name;&nbsp;&nbsp;&nbsp; for(let i = 0; i < sampleCart.length; i++) {&nbsp;&nbsp;&nbsp; &nbsp; if(sampleCart[i].price > expensive) {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; expensive = sampleCart[i].price;&nbsp; &nbsp; &nbsp; name = sampleCart[i].itemName;&nbsp; &nbsp; }&nbsp; }&nbsp; &nbsp; return name;}console.log(mostExpensiveItemName(sampleCart));

Smart猫小萌

这并不完全清楚,但似乎您正在寻找最昂贵的物品的名称。该代码似乎包含不少逻辑错误。function mostExpensiveItemName(sampleCart) {&nbsp; &nbsp; let expensive = 0;&nbsp; &nbsp; var name;&nbsp; &nbsp; for (let i = 0; i < sampleCart.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if (sampleCart[i].price > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; expensive += sampleCart[i].price;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name += sampleCart[i].itemName;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return name;}您将名称分配给变量,而您应该只返回项目的名称。function mostExpensiveItemName(sampleCart) {&nbsp; &nbsp; let mostExpensive = sampleCart[0];&nbsp; &nbsp; for (let i = 1; i < sampleCart.length; ++i) {&nbsp; &nbsp; &nbsp; &nbsp; if (sampleCart[i].price > mostExpensive.price) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; expensive = sampleCart[i];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return mostExpensive.itemName;}

Qyouu

最简单的方法是迭代数组 - 将价格与现有的最高价格进行比较,如果其更大则设置一个索引 - 然后使用该索引重新运行数组中的 itemName。const sampleCart = [&nbsp; {&nbsp; &nbsp; itemName: "Effective Programming Habits",&nbsp; &nbsp; type: "book",&nbsp; &nbsp; price: 13.99&nbsp; },&nbsp; {&nbsp; &nbsp; itemName: "Creation 3005",&nbsp; &nbsp; type: "computer",&nbsp; &nbsp; price: 299.99&nbsp; },&nbsp; {&nbsp; &nbsp; itemName: "Finding Your Center",&nbsp; &nbsp; type: "book",&nbsp; &nbsp; price: 15.00&nbsp; }];function mostExpensiveItemName(arr) {&nbsp; var mostExpensive = 0;&nbsp; var mostExpensiveIndex = 0;&nbsp;arr.forEach(function(item, index) {&nbsp; if(item.price > mostExpensive) {&nbsp; &nbsp;mostExpensive = item.price;&nbsp; &nbsp;mostExpensiveIndex = index;&nbsp; }&nbsp;})&nbsp; &nbsp; return sampleCart[mostExpensiveIndex].itemName;}console.log(mostExpensiveItemName(sampleCart)); // gives Creation 3005

慕标琳琳

const sampleCart = [&nbsp; {&nbsp; &nbsp; itemName: "Effective Programming Habits",&nbsp; &nbsp; type: "book",&nbsp; &nbsp; price: 13.99&nbsp; },&nbsp; {&nbsp; &nbsp; itemName: "Creation 3005",&nbsp; &nbsp; type: "computer",&nbsp; &nbsp; price: 299.99&nbsp; },&nbsp; {&nbsp; &nbsp; itemName: "Finding Your Center",&nbsp; &nbsp; type: "book",&nbsp; &nbsp; price: 15.0&nbsp; }];function mostExpensiveItemName(sampleCart) {&nbsp; // lets find the biggest number&nbsp; const biggestPrice = Math.max(...sampleCart.map((item) => item.price));&nbsp; return sampleCart.find((item) => item.price === biggestPrice).itemName;}console.log(mostExpensiveItemName(sampleCart));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript