Return a new array without the most expensive thing in the list. Your function should preserve the order of the items in this array. The code is correct and so is the output. I don't have a lot of exp with HOF so I would like to see how this can be done using the filter method.
function removeMostExpensive(list) {
var expensive = list[0];
for (var i = 1; i < list.length; i++) {
if (list[i].price > expensive.price) {
expensive = list[i];
}
}
var newList = [];
for (var i = 0; i < list.length; i++) {
if (list[i] !== expensive) {
newList.push(list[i]);
}
}
return newList;
}
console.log(removeMostExpensive([
{
item: "rice",
price: 12.75,
weightInPounds: 20
},
{
item: "chicken",
price: 6.99,
weightInPounds: 1.25
},
{
item: "celery",
price: 3.99,
weightInPounds: 2
},
{
item: "carrots",
price: 2.85,
weightInPounds: 2
},
{
item: "green beans",
price: 2.55,
weightInPounds: 2
}
]));