Pig Latin Translator - JavaScript

后端 未结 10 1786
别跟我提以往
别跟我提以往 2020-12-07 04:09

So for my cit class I have to write a pig Latin converter program and I\'m really confused on how to use arrays and strings together. The rules for the conversion are simpl

10条回答
  •  醉梦人生
    2020-12-07 04:57

    Here's my solution

    function translatePigLatin(str) {
      var newStr = str;
      // if string starts with vowel make 'way' adjustment
      if (newStr.slice(0,1).match(/[aeiouAEIOU]/)) {
        newStr = newStr + "way";
      }
      // else, iterate through first consonents to find end of cluster
      // move consonant cluster to end, and add 'ay' adjustment
      else {
        var moveLetters = "";
        while (newStr.slice(0,1).match(/[^aeiouAEIOU]/)) {
          moveLetters += newStr.slice(0,1);
          newStr = newStr.slice(1, newStr.length);
        }
        newStr = newStr + moveLetters + "ay";
      }
      return newStr;
    }
    

提交回复
热议问题