0

I have a function that takes a string of numbers and returns the highest and lowest number.

Function:

function highAndLow(numbers){

      var split = numbers.split(' ').map(Number);
      var min = Math.min.apply(null, split).toString()
      var max = Math.max.apply(null, split).toString()
      var empty = ""
      var x = empty.concat(max, min).split("").join("");
      return x
    }

    highAndLow("1 9 3 4 -5"); // return "9 -5"

The problem is at the moment it is returning "9-5" and I need it to return "9 -5"

monkeybanana
  • 27
  • 1
  • 6
  • You can concatenate strings with the `+` operator: `var x = max + " " + min;` – Pointy Dec 02 '17 at 14:17
  • Reason for downvoting me? What is the point of doing it without telling me why? Complete nonsense. – monkeybanana Dec 02 '17 at 14:22
  • If you're here for answers, don't worry about votes. –  Dec 02 '17 at 14:27
  • Don’t worry about downvotes. Likely someone felt you had not tried hard enough to figure this rather trivial issue out. For example split and join on the same thing does absolutely nothing, and concat is normally used on arrays – mplungjan Dec 02 '17 at 15:23

5 Answers5

1

You don't need a split and join. Just use the concat to add the space.

function highAndLow(numbers) {

  var split = numbers.split(' ').map(Number);
  var min = Math.min.apply(null, split).toString()
  var max = Math.max.apply(null, split).toString()
  var x = max.concat(" ", min);
  return x
}

console.log(highAndLow("1 9 3 4 -5"));

Or a more succinct version of your code, using modern features, is this:

function highAndLow(numbers) {
  var split = numbers.split(' ');
  return "" + Math.max(...split) + " " + Math.min(...split);
}

console.log(highAndLow("1 9 3 4 -5"));

No need to .map to Number because min and max will do the conversion for you. I used the spread syntax to pass the array, but you can use .apply if you're not transpiling for old implementations.

  • Or just use a simple number sort to run on any browser – mplungjan Dec 02 '17 at 15:44
  • @mplungjan: True, sorting would work, but I personally avoid it because it's usually a much slower operation. These will work on all browsers, except for the spread, which can be resolved by reverting to the `.apply()` version. –  Dec 02 '17 at 15:47
  • I wonder if the sort is as fragile as the spread on large arrays: https://stackoverflow.com/a/39719431/295783 – mplungjan Dec 02 '17 at 15:50
  • @mplungjan: I'd wager that there's a decent chance that it has not yet been fully optimized in some implementations, but that wouldn't be inherent to it as a feature. Sorting, while certainly capable of being optimized too, is still going to generally be a more complex operation. –  Dec 02 '17 at 15:50
  • https://jsperf.com/es6-add-element-to-create-new-array-concat-vs-spread-op – mplungjan Dec 02 '17 at 15:51
  • @mplungjan: That's a good point about large arrays, though I wouldn't let it keep me from using it unless I knew it was a possibility. The answer you linked is performing a replacement (via `.concat()`) instead of a mutation, so I don't see the operations as equivalent. –  Dec 02 '17 at 15:52
  • Sure - but you mentioned spread being faster than sort - I would doubt until I saw a jsPerf – mplungjan Dec 02 '17 at 15:55
  • @mplungjan: Yeah, I wouldn't worry about those kind of performance numbers. There's no reason one would be more able to be optimized than the other. It's just a matter of time before newer constructs get the same optimizations. –  Dec 02 '17 at 15:56
  • ...and I said sort is usually a slower operation. This isn't based on the current state of implementations, but rather on the minimal necessary algorithms required to accomplish each task, which are vastly different. –  Dec 02 '17 at 15:57
  • 1
    @mplungjan: I found a jsPerf that compares `Math.max` with `.apply()` to using `.sort()` if you're interested: https://jsperf.com/sort-vs-max/3 –  Dec 02 '17 at 16:05
0

concat a space in between like,

function highAndLow(numbers){
    
          var split = numbers.split(' ').map(Number);
          var min = Math.min.apply(null, split).toString()
          var max = Math.max.apply(null, split).toString()
          var empty = ""
          var x = empty.concat(max," ",min);
            return x;
        }
    
        console.log(highAndLow("1 9 3 4 -5")); // return "9 -5"
mplungjan
  • 169,008
  • 28
  • 173
  • 236
vibhor1997a
  • 2,336
  • 2
  • 17
  • 37
0

Use string concatenation. In es5:

function highAndLow(numbers){

  var split = numbers.split(' ').map(Number);
  var min = Math.min.apply(null, split).toString()
  var max = Math.max.apply(null, split).toString()
  return max + " " + min;
}

highAndLow("1 9 3 4 -5"); 


// It returns "9 -5"
Saran
  • 1,435
  • 11
  • 11
0

Splitting your code out into separate functions will help simplify the parts of the whole:

function getMinAndMax(numbers) {
    return {
        min: Math.min.apply(null, numbers),
        max: Math.max.apply(null, numbers),
    };
}

function getMinAndMaxViaString(input) {
    var numbers = input.split(' ').map(Number);
    var minAndMax = getMinAndMax(numbers);
    return minAndMax.min + ' ' + minAndMax.max;
}

This allows you to use and modify each function, the original and the delegating one, independently.

To the original point of your question, simply concatenating the strings together with a space is sufficient.

Dan Lugg
  • 20,192
  • 19
  • 110
  • 174
0

This can be vastly simplified - for example this code which will run on ANY version of ANY JS-capable browser since IE5.5 so you can party like it's 1999

function highAndLow(numberString) {
  var split = numberString.split(" ").sort(function(a, b){return a - b});
  return split.pop() + " " + split.shift();
}

console.log(highAndLow("1 9 3 4 -5")); // return "9 -5"
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • I think this is a good example on what not to do. In addition to sort being almost the least efficient way to find min and max, this version is making about 4 times as many string to number conversions (`a - b`), and shift is a lot slower compared to `split[0]` – Slai Dec 02 '17 at 16:29
  • For 5 digits it is completely irrelevant and how many does `var split = numbers.split(' ').map(Number);` do? – mplungjan Dec 02 '17 at 16:31