-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSwapCaseII.js
146 lines (104 loc) · 3.29 KB
/
SwapCaseII.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
Using the JavaScript language, have the function SwapII(str) take the
str parameter and swap the case of each character. Then, if a letter
is between two numbers (without separation), switch the places of
the two numbers. For example: if str is "6Hello4 -8World, 7 yes3" the
output should be 4hELLO6 -8wORLD, 7 YES3.
*/
function SwapII(str) {
var strArr = str.split('');
var numAndLetter = false;
var letterAndNum = false;
var onlyLetters = true;
var objectAdd = 0;
var addedObj = true;
var sawNumber = false;
var sawSecondNumber = false;
var numAndLetterObj = {};
for (i = 0; i < strArr.length; i++){
console.log("i: " + i);
strArr[i] = String(strArr[i]);
if (strArr[i].match(/[ ]/)) {
onlyLetters = false;
strArr[i] = " ";
}
if (strArr[i].match(/[a-z]/)){
strArr[i] = strArr[i].toUpperCase();
} else {
strArr[i] = strArr[i].toLowerCase();
}
if (!strArr[i].match(/[a-zA-Z0-9]/)) {
onlyLetters = false;
}
if (strArr[i].match(/[0-9]/)){
if (sawNumber) {
sawSecondNumber = true;
} else {
sawNumber = true;
}
}
if (strArr[i+1] != undefined){
if (strArr[i].match(/[0-9]/) && strArr[i+1].match(/[a-zA-Z]/)) {
numAndLetter = true;
if (sawNumber && sawSecondNumber){
objectAdd = 0;
sawSecondNumber = false;
}
numAndLetterObj[objectAdd] = { 'number': strArr[i], 'ind': i };
objectAdd++;
var onlyLetters = true;
hitNumber = true;
sawNumber = false;
console.log(numAndLetterObj);
console.log("sawNumber: " + sawNumber);
console.log("sawSecondNumber: " + sawSecondNumber);
} else if (strArr[i].match(/[0-9]/) && strArr[i-1].match(/[a-zA-Z]/)){
letterAndNum = true;
numAndLetterObj[objectAdd] = { 'number': strArr[i], 'ind': i };
objectAdd++;
console.log(numAndLetterObj);
sawSecondNumber = false;
}
}
if ((strArr[i+1] == undefined) && strArr[i].match(/[0-9]/)){
console.log('here');
if (sawNumber && sawSecondNumber){
objectAdd = 0;
sawSecondNumber = false;
}
numAndLetterObj[objectAdd] = { 'number': strArr[i], 'ind': i };
objectAdd++;
var onlyLetters = true;
hitNumber = true;
sawNumber = false;
console.log(numAndLetterObj);
console.log("sawNumber: " + sawNumber);
console.log("sawSecondNumber: " + sawSecondNumber);
}
if (numAndLetterObj[0] != undefined && numAndLetterObj[1] != undefined) {
strArr[parseInt(numAndLetterObj[0].ind)] = parseInt(numAndLetterObj[1].number);
strArr[parseInt(numAndLetterObj[1].ind)] = parseInt(numAndLetterObj[0].number);
strArr[parseInt(numAndLetterObj[0].ind)] = String(strArr[parseInt(numAndLetterObj[0].ind)]);
strArr[parseInt(numAndLetterObj[1].ind)] = String(strArr[parseInt(numAndLetterObj[1].ind)]);
delete numAndLetterObj[0];
delete numAndLetterObj[1];
numAndLetter = false;
numAndLetter = false;
objectAdd = 0;
sawNumber = false;
sawSecondNumber = false;
}
console.log("objectAdd: " + objectAdd);
}
strStr = strArr.join('');
return strStr;
}
//SwapII("2S 6 du5d4e");
//SwapII("Hello -5LOL6");
//SwapII("He34lo7 -5LOL689");
//SwapII("COMM2a2");
//SwapII("abcdE 8numbers2");
//SwapII("l2et5ter");
//SwapII("yolO11");
SwapII("123gg" + "))((");
//indicate when having seen a number, no interruption before seeing another number