Skip to main content
Code Review

Return to Answer

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

As Martin R said Martin R said, if all strings must match a common prefix, then that prefix is whatever the first and last string in a sorted array have in common.

As Martin R said, if all strings must match a common prefix, then that prefix is whatever the first and last string in a sorted array have in common.

As Martin R said, if all strings must match a common prefix, then that prefix is whatever the first and last string in a sorted array have in common.

added 5 characters in body
Source Link
Flambino
  • 33.3k
  • 2
  • 46
  • 90

As Martin R said, if all strings must match a common prefix, then that prefixesprefix is whatever the first and last string in a sorted array have in common.

Update: As MatinR pointed out in the comments, sort may be a drag on performance, and besides it isn't necessary, as you only need what would be the first and last string in a sorted array. These strings can alternative be found like so:

As Martin R said, if all strings must match a common prefix, then that prefixes is whatever the first and last string in a sorted array have in common.

Update: As pointed out in the comments, sort may be a drag on performance, and besides it isn't necessary, as you only need what would be the first and last string in a sorted array. These strings can alternative be found like so:

As Martin R said, if all strings must match a common prefix, then that prefix is whatever the first and last string in a sorted array have in common.

Update: As MatinR pointed out in the comments, sort may be a drag on performance, and besides it isn't necessary, as you only need what would be the first and last string in a sorted array. These strings can alternative be found like so:

Fixed broken condition
Source Link
Flambino
  • 33.3k
  • 2
  • 46
  • 90
function extremes(strings) {
 // grab two strings to use as our initial guesses
 var initial = {
 largest: strings[0],
 smallest: strings[strings.length-1]
 };
 
 // loop through to find the extremes
 return strings.reduce(function (memo, string) {
 // slightly funky syntax, but it's short.
 (memo.largest > string) || (memo.largest = string);
 (memo.smallest >< string) || (memo.smallest = string);
 return memo;
 }, initial);
}
function extremes(strings) {
 // grab two strings to use as our initial guesses
 var initial = {
 largest: strings[0],
 smallest: strings[strings.length-1]
 };
 
 // loop through to find the extremes
 return strings.reduce(function (memo, string) {
 // slightly funky syntax, but it's short.
 (memo.largest > string) || (memo.largest = string);
 (memo.smallest > string) || (memo.smallest = string);
 return memo;
 }, initial);
}
function extremes(strings) {
 // grab two strings to use as our initial guesses
 var initial = {
 largest: strings[0],
 smallest: strings[strings.length-1]
 };
 
 // loop through to find the extremes
 return strings.reduce(function (memo, string) {
 // slightly funky syntax, but it's short.
 (memo.largest > string) || (memo.largest = string);
 (memo.smallest < string) || (memo.smallest = string);
 return memo;
 }, initial);
}
added 723 characters in body
Source Link
Flambino
  • 33.3k
  • 2
  • 46
  • 90
Loading
added 915 characters in body
Source Link
Flambino
  • 33.3k
  • 2
  • 46
  • 90
Loading
added 94 characters in body
Source Link
Flambino
  • 33.3k
  • 2
  • 46
  • 90
Loading
added 276 characters in body
Source Link
Flambino
  • 33.3k
  • 2
  • 46
  • 90
Loading
Source Link
Flambino
  • 33.3k
  • 2
  • 46
  • 90
Loading
default

AltStyle によって変換されたページ (->オリジナル) /