Skip to content

Commit 0963da8

Browse files
committed
Added easy/swap_case
1 parent 3043841 commit 0963da8

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

easy/swap_case.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Have the function swapCase(str) take the str parameter and swap the case of
3+
* each character. For example: if str is "Hello World" the output should be
4+
* hELLO wORLD. Let numbers and symbols stay the way they are.
5+
*
6+
* https://www.coderbyte.com/results/bhanson:Swap%20Case:JavaScript
7+
*
8+
* @param {string} str
9+
* @return {string}
10+
*/
11+
function swapCase(str) {
12+
const LOWER = 'abcdefghijklmnopqrstuvwxyz';
13+
const UPPER = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
14+
15+
const newString = str
16+
.split('')
17+
.map(char => {
18+
if (LOWER.includes(char)) {
19+
return char.toUpperCase();
20+
}
21+
22+
if (UPPER.includes(char)) {
23+
return char.toLowerCase();
24+
}
25+
26+
return char;
27+
})
28+
.join('');
29+
30+
return newString;
31+
}
32+
33+
module.exports = swapCase;

easy/swap_case.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const swapCase = require('./swap_case');
2+
3+
describe('swapCase()', () => {
4+
test('converts lowercase letters to uppercase', () => {
5+
expect(swapCase('abcz')).toBe('ABCZ');
6+
});
7+
8+
test('converts uppercase letters to lowercase', () => {
9+
expect(swapCase('ZABC')).toBe('zabc');
10+
});
11+
12+
test('correctly swaps cases', () => {
13+
expect(swapCase('the QUICK Brown foX')).toBe('THE quick bROWN FOx');
14+
});
15+
16+
test('ignores non alphabetic characters', () => {
17+
expect(swapCase('the $&@!QUICK &@!Brown&@! foX')).toBe(
18+
'THE $&@!quick &@!bROWN&@! FOx'
19+
);
20+
});
21+
});

0 commit comments

Comments
 (0)