-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8-2-21.js
More file actions
53 lines (46 loc) · 1.35 KB
/
Copy path8-2-21.js
File metadata and controls
53 lines (46 loc) · 1.35 KB
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
// The Vowel Code
// Step 1: Create a function called encode() to replace all the lowercase vowels in a given string with numbers according to the following pattern:
// a -> 1
// e -> 2
// i -> 3
// o -> 4
// u -> 5
// For example, encode("hello") would return "h2ll4". There is no need to worry about uppercase vowels in this kata.
// Step 2: Now create a function called decode() to turn the numbers back into vowels according to the same pattern shown above.
// For example, decode("h3 th2r2") would return "hi there".
// For the sake of simplicity, you can assume that any numbers passed into the function will correspond to vowels.
function encode(string) {
let alph = 'aeiou'
let vowels = {
'a': 1,
'e': 2,
'i': 3,
'o': 4,
'u': 5
}
let str = ''
for (let i = 0 ; i < string.length ; i++) {
if (alph.includes(string[i])) {
str += vowels[string[i]]
} else str += string[i]
} return str
}
function decode(string) {
let nums = [1,2,3,4,5]
let vowels = {
'a': 1,
'e': 2,
'i': 3,
'o': 4,
'u': 5
}
let str = ''
for (let i = 0 ; i < string.length ; i++) {
if (nums.includes(+string[i])) {
str += Object.keys(vowels).find(x=>vowels[x] === +string[i])
} else {
str += string[i]
}
}
return str
}