forked from alexf1225/coderbyte_js_solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcb_letterChanges.js
179 lines (150 loc) · 4.43 KB
/
cb_letterChanges.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//Using the JavaScript language, have the function LetterChanges(str) take the str parameter being passed and modify it using the following algorithm. Replace every letter in the string with the letter following it in the alphabet (ie. c becomes d, z becomes a). Then capitalize every vowel in this new string (a, e, i, o, u) and finally return this modified string.
function LetterChanges(str) {
// turn str into an array
var arr = str.split(""),
newStr = "";
for (var i in arr){
if (/[a-yA-Y]/.test(arr[i])) {arr[i] = String.fromCharCode(arr[i].charCodeAt(0)+1)}
if (/[aeiou]/.test(arr[i])) {arr[i] = arr[i].toUpperCase()}
if (arr[i] === "Z" || arr[i] === "z") {arr[i] = "A"}
}
newStr = arr.join("");
return newStr;
}
//My test
var str = "fun times!"
console.log(LetterChanges(str)); //solution: "gvO Ujnft!"
/* Non RegEx Solution
var letterChanges = function(str){
//function to confirm if myChar is a vowel
var isVowel = function(myChar){
var vowel = ["a","e","i","o","u"],
result = false;
for (var i in vowel){
if(myChar === vowel[i]){
result = true;
}
}
return result;
};
//function that returns the nextLetter (does not handle special cases, see coderbyte code)
var nextLetter = function(myChar){
var newCharCode = myChar.charCodeAt(0) + 1
return myChar.replace(/([a-zA-Z])/, String.fromCharCode(newCharCode))
};
//split the input string into an array
var arr = str.split(""),
result = "";
//evaluate each and convert to next letter
for (var i in arr){
if (arr[i] !== " "){
result += nextLetter(arr[i]);
}else{
result += " ";
}
}
//review the final and capitalize vowels
result = result.split("")
for (var i in result){
if (isVowel(result[i])=== true){
result.splice(i,1,result[i].toUpperCase())
}
}
str = result.join("");
return str;
};
str = "fun times!#";
console.log(letterChanges(str));
*/
/* Prior version, also non RegEx
function LetterChanges(str) {
// code goes here
//function to confirm if myChar is a vowel
var isVowel = function(myChar){
var vowel = ["a","e","i","o","u"],
result = false;
for (var i in vowel){
if(myChar === vowel[i]){
result = true;
}
}
return result;
};
//function that returns the nextLetter
var nextLetter = function(myChar){
return myChar.replace(/([a-zA-Z])[^a-zA-Z]*$/, function(a){
var c = a.charCodeAt(0);
switch(c){
//wraps A after Z and a after z
case 90: return 'A';
case 122: return 'a';
default: return String.fromCharCode(++c);
}
});
}
//split the input string into an array
var arr = str.split(""),
result = "";
//evaluate each and convert to next letter
for (var i in arr){
if (arr[i] !== " "){
result += nextLetter(arr[i]);
}else{
result += " ";
}
}
//review the final and capitalize vowels
result = result.split("")
for (var i in result){
if (isVowel(result[i])=== true){
result.splice(i,1,result[i].toUpperCase())
}
}
str = result.join("");
return str;
}
// keep this function call here
// to see how to enter arguments in JavaScript scroll down
print(LetterChanges(readline()));
*/
/* Another version w/ Regex
function LetterChanges(str) {
// code goes here
//filter to check for letter
var isLetter = function(testChar){
var alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var arr = alpha.split("");
for (var i in arr){
if (testChar === arr[i]){
return true;
}
}
return false;
};
//redo str w/o weird characters
var strArr = str.split(""),
finalStr = "",
newCharCode = 0;
for (var i in strArr){
if (isLetter(strArr[i])){
newCharCode = strArr[i].charCodeAt(0) + 1;
finalStr += String.fromCharCode(newCharCode);
}else{
finalStr += strArr[i];
}
}
//capitalize vowels
var semi = finalStr.split("");
var str2 = "";
for (var i in semi){
if (semi[i] === "a" || semi[i] === "e" || semi[i] === "i" || semi[i] === "o" || semi[i] === "u"){
semi[i] = semi[i].toUpperCase();
}
}
str2 = semi.join("");
return str2;
}
// keep this function call here
// to see how to enter arguments in JavaScript scroll down
print(LetterChanges(readline()));
*/