制作猪犊转换器的麻烦

时间:2015-11-16 04:01:39

标签: javascript if-statement for-loop slice charat

好吧所以我在尝试将此内容删除到第一个元音之前的所有内容,将其添加到最后,然后在该结尾添加“ay”时遇到了一些麻烦

    function translate(word) { // put first constants at end && add "ay" 
      var result = word;
      var end = "WillBeOverrideN";
      for (var i = 0; i < result.length; i++) {
        if (result.charAt(i) === ("a" || "e" || "i" || "o" || "u")) {
          end = result.slice(0, i-1); // everything b/f vowel
          result = result.slice(i-1); // everything after && vowel 
          result += end; 
          break; 
        }
      }
      result += "ay";
      return result;
    }

1 个答案:

答案 0 :(得分:0)

你必须稍微改变功能, 这是工作职能

function translate(word) { // put first constants at end && add "ay" 
      var result = word;
      var last = "WillBeOverrideN";
      for (var i = 0; i < result.length; i++) {
        if(["a","e","i","o","u"].indexOf(result[i]) !== -1){      

          last = result.slice(0, i); // everything b/f vowel
          result = result.slice(i); // everything after && vowel 
          result += last ; 
          break; 
        }
      }
      result += "ay";
      return result;
    }