Can someone help me with the Problem below

Create a function named mostExpensiveItemName to find the name of the highest priced item in the shopping cart .

The function should

  • accept an array of items as its argument
  • return the name of the most expensive item, as a string

As shown in the example shopping cart data below, the shopping cart is an array of items. Each item is an object with keys itemName , type , and price .

Your function mostExpensiveItemName should find the highest priced item in the cart, and return its itemName .

For example, passing in the sample array items , your function should return "Creation 3005" .

mostExpensiveItemName(items) //=> "Creation 3005"

Note : if there are no items in the array passed in, your mostExpensiveItemName should return undefined .

Example Shopping Cart Data

let items = [
  {
    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
  }
]

Note : The tests will pass different arrays into your function - not just this one. The arrays will be like this one. Each item will be an object with the keys itemName , type , and price .

This is the code i have but i get the error message (should return “undefined” for an empty cart
TypeError: Cannot read property ‘itemName’ of undefined
at mostExpensiveItemName)

function mostExpensiveItemName(items){
let indexOfExensiveItem =0; //to store index of expensive item
for(var i=0;i<items.length;i++){
if(items[i].price > items[indexOfExensiveItem].price){
indexOfExensiveItem = i;
}
}
return items[indexOfExensiveItem].itemName;
}

Pretty much exactly as it says, it should return undefined for an empty cart, however at the moment if the cart is empty it throws an error at the return step. Consider implementing some validation around this idea to prevent an empty cart from causing an error.

You are getting the error for an empty cart because items[indexOfExensiveItem] is undefined, and since it’s undefined, it can’t get the value of items[indexOfExensiveItem].itemName.
You can solve this problem with two options:

  1. Use the optional chaining operator (?.) or
  2. Use an if statement.

Using the optional chaining operator

function mostExpensiveItemName(items){
    if(items.length = 0) return undefined
    let indexOfExensiveItem =0; //to store index of expensive item
    for(var i=0;i<items.length;i++){
        if(items[i].price > items[indexOfExensiveItem].price){
            indexOfExensiveItem = I;
        }
    }
    return items[indexOfExensiveItem]?.itemName; // notice the ?. here
}

Using an if statement

function mostExpensiveItemName(items){
    if(items.length = 0) return undefined
    let indexOfExensiveItem =0; //to store index of expensive item
    for(var i=0;i<items.length;i++){
        if(items[i].price > items[indexOfExensiveItem].price){
            indexOfExensiveItem = I;
        }
    }
    if(items[indexOfExensiveItem]){
        return items[indexOfExensiveItem].itemName
    }
}
1 Like