Skip to content

Commit 2101b30

Browse files
committed
Create Swap Case
1 parent d91057a commit 2101b30

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

Swap Case

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/***************************************************************************************
2+
* *
3+
* CODERBYTE BEGINNER CHALLENGE *
4+
* *
5+
* Swap Case *
6+
* Using the JavaScript language, have the function SwapCase(str) take the str *
7+
* parameter and swap the case of each character. For example: if str is "Hello World" *
8+
* the output should be hELLO wORLD. Let numbers and symbols stay the way they are. * *
9+
* *
10+
* SOLUTION *
11+
* I am going to be using a new string to store my modified results. I am going to *
12+
* loop thru every character and covert it to UpperCase. Then I am going to compare *
13+
* that to the character in the string. If they are the same - meaning both are *
14+
* uppercase then I am going to return the lowercase of the character. If they are not *
15+
* the same then return the Uppercase character.
16+
* *
17+
* Steps for solution *
18+
* 1) Create new string to hold my results *
19+
* 2) Loop thru each character in the string *
20+
* 3) Save the uppercase of this character in u *
21+
* 4) Compare uppercase to current character *
22+
* 5) If they are the same then return the lowercase *
23+
* 6) Else return uppercase *
24+
* *
25+
***************************************************************************************/
26+
27+
function SwapCase(str) {
28+
29+
var result = '';
30+
31+
for (var i = 0; i < str.length; i++) {
32+
var c = str[i];
33+
var u = c.toUpperCase();
34+
35+
result += u === c ? c.toLowerCase() : u;
36+
}
37+
38+
return result;
39+
40+
}

0 commit comments

Comments
 (0)