-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathletter-changes.js
46 lines (41 loc) · 1.11 KB
/
letter-changes.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
//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 (i.e. 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.
const alphabet = [
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h",
"i",
"j",
"k",
"l",
"m",
"n",
"o",
"p",
"q",
"r",
"s",
"t",
"u",
"v",
"w",
"x",
"y",
"z",
]
const vowels = 'aeiou';
const letterChanges = (string) => {
let changedString = '';
for (let index = 0; index < string.length; index++) {
const currentLetter = string[index];
const currentLetterIndex = alphabet.indexOf(currentLetter);
const newLetter = currentLetterIndex === 25 ? alphabet[0] : alphabet[currentLetterIndex + 1];
changedString = changedString + (vowels.includes(newLetter) ? newLetter.toUpperCase() : newLetter);
}
return changedString
}
console.log(letterChanges('abcz'));