Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a shorter way of solving this using higher order functions? Perhaps the filter method?

Tags:

javascript

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
  }
]));
like image 845
Ron Skinner Avatar asked Mar 20 '21 05:03

Ron Skinner


People also ask

Is a filter a higher-order function?

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 .

What is a higher-order function and why are they so useful?

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.

What are the advantages of higher-order functions?

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.

Which of the higher-order functions can help you calculate the sum of an array of numbers?

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.


2 Answers

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.

like image 182
CertainPerformance Avatar answered Oct 16 '22 13:10

CertainPerformance


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.

like image 23
Ashish Yadav Avatar answered Oct 16 '22 11:10

Ashish Yadav