897 B
897 B
title, tags, expertise, author, cover, firstSeen, lastUpdated
| title | tags | expertise | author | cover | firstSeen | lastUpdated |
|---|---|---|---|---|---|---|
| Swapcase string | string | beginner | maciv | blog_images/mountain-lake-2.jpg | 2020-11-15T13:09:03+02:00 | 2020-11-15T13:09:03+02:00 |
Creates a string with uppercase characters converted to lowercase and vice versa.
- Use the spread operator (
...) to convertstrinto an array of characters. - Use
String.prototype.toLowerCase()andString.prototype.toUpperCase()to convert lowercase characters to uppercase and vice versa. - Use
Array.prototype.map()to apply the transformation to each character,Array.prototype.join()to combine back into a string. - Note that it is not necessarily true that
swapCase(swapCase(str)) === str.
const swapCase = str =>
[...str]
.map(c => (c === c.toLowerCase() ? c.toUpperCase() : c.toLowerCase()))
.join('');
swapCase('Hello world!'); // 'hELLO WORLD!'