-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalternating case.js
More file actions
16 lines (14 loc) · 957 Bytes
/
Copy pathalternating case.js
File metadata and controls
16 lines (14 loc) · 957 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// altERnaTIng cAsE <=> ALTerNAtiNG CaSe
// Define String.prototype.toAlternatingCase (or a similar function/method such as to_alternating_case/toAlternatingCase/ToAlternatingCase in your selected language; see the initial solution for details) such that each lowercase letter becomes uppercase and each uppercase letter becomes lowercase. For example:
// "hello world".toAlternatingCase() === "HELLO WORLD"
// "HELLO WORLD".toAlternatingCase() === "hello world"
// "hello WORLD".toAlternatingCase() === "HELLO world"
// "HeLLo WoRLD".toAlternatingCase() === "hEllO wOrld"
// "12345".toAlternatingCase() === "12345" // Non-alphabetical characters are unaffected
// "1a2b3c4d5e".toAlternatingCase() === "1A2B3C4D5E"
String.prototype.toAlternatingCase = function () {
return this.split('').map(el=> {
if (el.toUpperCase() == el) return el.toLowerCase()
else return el.toUpperCase()
}).join('')
}