'How to add toAlternatingCase custom method to String class in JavaScript?

I'm trying to add a custom method to the String class in JavaScript called toAlternatingCase, it will turn lowercase characters into uppercase and vice versa in the string that is called on, I'm trying to make it like the built-in toUpperCase/toLowerCase methods which don't take any arguments. this is a kata(challenge) on codewars.



Solution 1:[1]

String.prototype.toAlternatingCase = function () {
  return this.split("").map(l => l === l.toUpperCase() ? l.toLowerCase() : l.toUpperCase()).join("");
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ryan M