Pig Latin Translator - JavaScript

后端 未结 10 1793
别跟我提以往
别跟我提以往 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 05:16

    This code is basic, but it works. First, take care of the words that start with vowels. Otherwise, for words that start with one or more consonants, determine the number of consonants and move them to the end.

    function translate(str) {
        str=str.toLowerCase();
    
        // for words that start with a vowel:
        if (["a", "e", "i", "o", "u"].indexOf(str[0]) > -1) {
            return str=str+"way";
        }
    
        // for words that start with one or more consonants
       else {
       //check for multiple consonants
           for (var i = 0; i -1){
                   var firstcons = str.slice(0, i);
                   var middle = str.slice(i, str.length);
                   str = middle+firstcons+"ay";
                   break;}
                }
        return str;}
    }
    
    translate("school");
    

提交回复
热议问题