File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments