-1

So I have here some JS code for getting either a singular value or, if there's a 'range' of prices separated by ' - ', getting the highest value:

function getPrice() {
  var str = ("£25 - £36");

  var val;
  if (str.indexOf('-') > -1) {
    val = Number(str.replace(/[^0-9.-]+/g, "").split('-')[1], 10);
  } else {
    val = Number(str.replace(/[^0-9.-]+/g, "").split('-')[0], 10);
  }
  return val;
}

How would I modify this so that it gets the lowest value instead?

This 'range' will always be either in the format of £25 or £13 - £72 and it will always be in pounds. The highest value will always be the last; in this context they are functionally the same.

Weirdali
  • 413
  • 1
  • 7
  • 16
  • _"...getting the highest value"_ - Only if the range will always be ` - ` – Andreas Sep 10 '21 at 08:53
  • What's the actual problem here? `getPrice()` is not that complicated in what it does. The "mechanic" to get the _"highest value"_ is just an index... (in fact, just get rid of the "range" part) – Andreas Sep 10 '21 at 08:55
  • As Andreas says, this gets the **last** value, regardless of whether it's actually the largest, if the order can vary then you will probably want to take a slightly different approach after splitting your string. – DBS Sep 10 '21 at 08:56
  • Is this always going to be in pounds? Can the pound sign be on the other side? Is the input always going to be `£value` or `£value - £value`? – nick zoum Sep 10 '21 at 08:57
  • I have updated the question the confirm that this 'range' will always be in this format @Andreas – Weirdali Sep 10 '21 at 09:02
  • That's nice of you. But there's still no indication of research or your attempt to solve this on your own. As I've written in my second comment, `getPrice()` is really not complicated in what it does. And with those added restrictions on the possible input its really just a matter of removing code... – Andreas Sep 10 '21 at 09:04
  • @nickzoum The value will always be in pounds in the format given – Weirdali Sep 10 '21 at 09:05
  • If it's always in this format, then what does your code actually do? It says: if there's a `-` then get the second number... so change it to get the first instead of the second. Are you saying you don't know what `[1]` does? – freedomn-m Sep 10 '21 at 09:10

1 Answers1

3

One way to achieve what you require would be to use a Regex to pull out all the figures in the string. You can then apply those matches to Math.min to find the lowest one.

The benefit of this approach is that it will retrieve the lowest number regardless of its position in the string.

function getLowestPrice(input) {
  let amounts = input.match(/\d{1,3}(,\d{3})*(\.\d+)?/g);
  amounts = amounts.map(v => v.replace(',', '')); // remove comma grouping
  return Math.min.apply(this, amounts);
}

console.log(getLowestPrice('£25 - £36')); // = 25
console.log(getLowestPrice('£1,090.99 - £77.25')); // = 77.25

Edit - I just expanded the Regex to handle values containing thousands grouping and decimals, credit to this answer for that.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339