Skip to content

Commit 214cbf2

Browse files
committed
Solve letterChanges
1 parent 2cc776c commit 214cbf2

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

src/Letter_Changes.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Description: For this challenge you will be manipulating characters in a
2+
// string based off their positions in the alphabet.
3+
4+
// Q. Have the function LetterChanges(str) take the str parameter being passed
5+
// and modify it using the following algorithm. Replace every letter in the
6+
// string with the letter following it in the alphabet
7+
// (ie. c becomes d, z becomes a). Then capitalize every vowel in this new
8+
// string (a, e, i, o, u) and finally return this modified string.
9+
10+
// Examples
11+
// Input: "hello*3"
12+
// Output: Ifmmp*3
13+
14+
// Input: "fun times!"
15+
// Output: gvO Ujnft!
16+
17+
const letterChanges = str => {
18+
const alphabets = [
19+
'a',
20+
'b',
21+
'c',
22+
'd',
23+
'e',
24+
'f',
25+
'g',
26+
'h',
27+
'i',
28+
'j',
29+
'k',
30+
'l',
31+
'm',
32+
'n',
33+
'o',
34+
'p',
35+
'q',
36+
'r',
37+
's',
38+
't',
39+
'u',
40+
'v',
41+
'w',
42+
'x',
43+
'y',
44+
'z'
45+
];
46+
47+
const vowels = ['a', 'e', 'i', 'o', 'u'];
48+
49+
const arr = str.toLowerCase().split('');
50+
const newArr = [];
51+
52+
for (let i = 0; i < arr.length; i++) {
53+
for (let j = 0; j < alphabets.length; j++) {
54+
if (arr[i] === alphabets[j]) {
55+
let value = alphabets[j !== 25 ? j + 1 : 0];
56+
57+
for (let k = 0; k < vowels.length; k++) {
58+
if (value === vowels[k]) {
59+
value = value.toUpperCase();
60+
break;
61+
}
62+
}
63+
64+
newArr.push(value);
65+
break;
66+
} else if (j === alphabets.length - 1) {
67+
newArr.push(arr[i]);
68+
}
69+
}
70+
}
71+
72+
const newStr = newArr.join('');
73+
return newStr;
74+
};
75+
76+
console.log(letterChanges('hello*3'));

0 commit comments

Comments
 (0)