-
Notifications
You must be signed in to change notification settings - Fork 0
/
kata.js
44 lines (40 loc) · 1.53 KB
/
kata.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
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 consonants = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'];
const vowels = ['a', 'e', 'i', 'o', 'u'];
//#1
function replaceLetters(word) {
return word.split('').map(i => {
idx = alphabet.indexOf(i);
if (vowels.includes(i)) {
return alphabet.at(idx - 1);
} else {
for (let i = 1; i < 26; i++) {
if (idx + i > 25) {
idx = 0;
i = 0;
}
if (vowels.includes(alphabet.at(idx + i))) {
return alphabet.at(idx + i)
}
}
}
}).join('')
}
// # 2
// function replaceLetters(word) {
// return [...word].map(l =>{
// if( consonants.includes(l) ){
// let nextL = alphabet.slice( alphabet.indexOf(l) )
// .find( el => vowels.includes(el) );
// return nextL || 'a';
// } else if( vowels.includes(l) ) {
// let precL = alphabet.slice(0, alphabet.indexOf(l) )
// .reverse()
// .find( el => consonants.includes(el) );
// return precL || 'z';
// }
// }).join``;
// }
console.log(replaceLetters('cat')); // 'ezu'
console.log(replaceLetters('codewars')); // 'enedazuu'
console.log(replaceLetters('abcdtuvwxyz')); // 'zeeeutaaaaa'