Pig Latin

Translate the provided string to pig latin.

Pig Latin takes the first consonant (or consonant cluster) of an English word, moves it to the end of the word and suffixes an “ay”.

If a word begins with a vowel you just add “way” to the end.

Input strings are guaranteed to be English words in all lowercase.

输入一个字符串,如果首字母为辅音,移到尾部,加后缀ay,如果为元音’aeiou’,加后缀way


function translatePigLatin(str) {
  var arr = str.split('');
  var reg = 'aeiou';
  if (reg.indexOf(arr[0]) !== -1) {
    arr.push('way');
    return arr.join('');
  } else {
    for (var i = 0; ilength; i++) {
      if (reg.indexOf(arr[0]) === -1) {
        var first = arr.shift();
        arr.push(first);
      } else {
        arr.push('ay');
        return arr.join('');
      }
    }
  }

}

translatePigLatin("consonant");

你可能感兴趣的:(leetcode)