Plain JavaScript doesn't offer much help here, even though a number of libraries provide case-preserving string methods.

We'll solve this problem by writing some functional code. We will some single-purpose, reusable, and pure functions. Pure in this case means the output of the functions depends only on it's input. No magic, just a predictable function in the mathematical sense. We will then compose them together in a solution that's easy to read and debug.

So here are our building blocks:

detect if a string is uppercase or lowercase

compare the case of two strings

switch the case of a string

With this functionality in place we can solve this challenge! And because those functions are not tied to our particular problem we can reuse them later, or in an entirely different project. We could even make them available for other people, for example by publishing a package on npm.

Detect a string's case

Let's just return a boolean indicating whether a string's first character is uppercase.