Skip to content

Commit 257968e

Browse files
committed
The Vowel Code
1 parent 75ad611 commit 257968e

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

6_kyu/theVowelCode.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
## [The Vowel Code](https://www.codewars.com/kata/53697be005f803751e0015aa)
2+
3+
- Step 1: Create a function called `encode()` to replace all the lowercase vowels in a given string with numbers according to the following pattern:
4+
5+
```js
6+
a -> 1
7+
e -> 2
8+
i -> 3
9+
o -> 4
10+
u -> 5
11+
```
12+
13+
- For example, `encode("hello")` would return `"h2ll4"`. There is no need to worry about uppercase vowels in this kata.
14+
15+
<b>Step 2:<b> Now create a function called `decode()` to turn the numbers back into vowels according to the same pattern shown above.
16+
17+
- For example, `decode("h3 th2r2")` would return `"hi there"`.
18+
19+
- For the sake of simplicity, you can assume that any numbers passed into the function will correspond to vowels.
20+
21+
#### Solution:
22+
23+
```js
24+
function encode(string) {
25+
const vowelToNumber = {
26+
'a': '1', 'e': '2', 'i': '3', 'o': '4', 'u': '5'
27+
};
28+
return string.replace(/[aeiou]/g, match => vowelToNumber[match]);
29+
}
30+
31+
function decode(string) {
32+
const numberToVowel = {
33+
'1': 'a', '2': 'e', '3': 'i', '4': 'o', '5': 'u'
34+
};
35+
return string.replace(/[1-5]/g, match => numberToVowel[match]);
36+
}
37+
38+
console.log(encode("hello")); // 'h2ll4'
39+
console.log(encode("How are you today?")); // 'H4w 1r2 y45 t4d1y?'
40+
console.log(decode("h2ll4")); // 'hello'
41+
console.log(decode("H4w 1r2 y45 t4d1y?")); // 'How are you today?'
42+
```

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,5 +679,12 @@ Solutions to CodeWars programming tasks. I try to update every day
679679
<td><a href="5 kyu"> </a></td>
680680
<td><a href="others "></a></td>
681681
</tr>
682+
<tr>
683+
<td><a href=" "> </a></td>
684+
<td><a href="7 kyu"> </a></td>
685+
<td><a href="https://github.com/esadakman/javaScript-coding-challenges/blob/master/6_kyu/theVowelCode.md">theVowelCode</a></td>
686+
<td><a href="5 kyu"> </a></td>
687+
<td><a href="others "></a></td>
688+
</tr>
682689
</table>
683690
</p>

0 commit comments

Comments
 (0)