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
}
]));
In functional programming, filter is a higher-order function that processes a data structure (usually a list) in some order to produce a new data structure containing exactly those elements of the original data structure for which a given predicate returns the boolean value true .
Higher Orders Functions are functions that perform operations on other functions. In this definition, operations can mean taking one or more functions as an argument OR returning a function as the result. It doesn't have to do both. Doing one or the other qualifies a function as a higher order function.
Benefits of higher-order functionsThey provide simplicity to the code, allowing the programmer and any other party to understand the code at a high level easily. They make it less time-consuming to write fully functioning and clear code as it's easier to debug when higher-order functions are used.
Additionally, the concept of the higher-order function allows composability of functions. For example, you compose calculate() with sum() to calculate the sum of all numbers in an array.
You can find the most expensive item by mapping the array to the prices and calling Math.max
on it. Then filter the array by whether the current item being iterated over has that price:
function removeMostExpensive(list) {
const max = Math.max(...list.map(obj => obj.price));
return list.filter(obj => obj.price !== max);
}
That's assuming that your current code works - that if more than one item is tied for the same highest expense, all should be removed.
There won't be much difference with filter
but you can do both operation ( finding max & returning the array without max ) on same loop.
Solution without extra memory allocation:
function removeMostExpensive(list) {
const newList = [];
let expensive = list[0];
list.filter((item, index) => {
if(item.price > expensive.price) {
expensive = list[index];
}
// because the expensive will always change if above condition is true
if(item !== expensive) {
newList.push(list[index]);
}
});
return newList;
}
I have only compared your output from the sample input with the above modified version of code. Not sure if this will pass all the test cases but you can simplify your code this way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With