[JS Practice] Changing Vowel(替换单词中的元音字母)

Title :

Changing Vowel(替换单词中的元音字母)

Description:

要求实现两个方法,第一个方法在接收到一个包含元音的英文单词后会把元音替换成相应的数字,第二个方法则反过来,会把相应的数字替换成对应的元音单词。
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.

My solution (used switch/case) :

// turn vowels into numbers
// 方法把元音替换成数字
function encode(string){
  // Put each character into an array element
  // 把单词劈成由每个字母组成的一个数组
  var arr = string.split("");
  var index;
  
  // Boucle array 
  // 循环每个字符,如果遇到相应的元音则替换掉
  arr.forEach(function(element){
    switch(element) {
      case "a" :
        index = arr.indexOf(element);
        arr[index] = 1;
        break;
      case "e" :
        index = arr.indexOf(element);
        arr[index] = 2;
        break;
      case "i" :
        index = arr.indexOf(element);
        arr[index] = 3;
        break;
      case "o" :
        index = arr.indexOf(element);
        arr[index] = 4;
        break;
      case "u" :
        index = arr.indexOf(element);
        arr[index] = 5;
        break;
      default :
        break;
    }
  });
  // 合并数组
  return arr.join("");
}

//turn numbers back into vowels
// 方法把数字替换成元音
function decode(string){
  // The same theory, I just changed the switch/case
  // Put each character into an array element
  var arr = string.split("");
  var index;
  
  // Boucle array 
  arr.forEach(function(element){
    switch(element) {
      case "1" :
        index = arr.indexOf(element);
        arr[index] = "a";
        break;
      case "2" :
        index = arr.indexOf(element);
        arr[index] = "e";
        break;
      case "3" :
        index = arr.indexOf(element);
        arr[index] = "i";
        break;
      case "4" :
        index = arr.indexOf(element);
        arr[index] = "o";
        break;
      case "5" :
        index = arr.indexOf(element);
        arr[index] = "u";
        break;
      default :
        break;
    }
  });
  return arr.join("");

Another solution, with replace function, but shorter :

function encode(string){
  return string.replace(/[aeiou]/g, function (x) { return '_aeiou'.indexOf(x) });
}

function decode(string){
  return string.replace(/[1-5]/g, function (x) { return '_aeiou'.charAt(x) });
}

Ref. from codewars

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s