JS Built-ins
StringString.prototype.localeCompareintermediate

String.prototype.localeCompare() β€” sign of result

πŸ“– Usage Example
'apple'.localeCompare('banana')   // β†’ negative
'banana'.localeCompare('apple')   // β†’ positive
'abc'.localeCompare('abc')        // β†’ 0

Use localeCompare() to compare strings in a locale-aware way, returning negative, zero, or positive.

String.prototype.localeCompare() β€” Result Sign

The exact number returned is implementation-defined, but the sign is specified: negative, zero, or positive.

Challenge: Implement compareSign(a, b) that returns -1, 0, or 1 based on the sign of a.localeCompare(b).

compareSign('a', 'b') // β†’ -1
compareSign('b', 'b') // β†’ 0
compareSign('c', 'b') // β†’ 1
πŸ’‘ Hints (1)
  • β€’ Normalize with: `if (r < 0) return -1; if (r > 0) return 1; return 0`.