Files
30-seconds-of-code/snippets/swapCase.md
Angelos Chalaris 8a6b73bd0c Update covers
2023-02-16 22:24:28 +02:00

26 lines
847 B
Markdown

---
title: Swapcase string
tags: string
cover: mountain-lake-2
firstSeen: 2020-11-15T13:09:03+02:00
lastUpdated: 2020-11-15T13:09:03+02:00
---
Creates a string with uppercase characters converted to lowercase and vice versa.
- Use the spread operator (`...`) to convert `str` into an array of characters.
- Use `String.prototype.toLowerCase()` and `String.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`.
```js
const swapCase = str =>
[...str]
.map(c => (c === c.toLowerCase() ? c.toUpperCase() : c.toLowerCase()))
.join('');
```
```js
swapCase('Hello world!'); // 'hELLO WORLD!'
```