Pig Latin Translator - JavaScript

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

    this is my solution code :

    function translatePigLatin(str) {
    var vowel;
    var consonant;
    var n =str.charAt(0);
    vowel=n.match(/[aeiou]/g);
    if(vowel===null)
    {
    consonant= str.slice(1)+str.charAt(0)+”ay”;
    }
    else
    {
    consonant= str.slice(0)+”way”;
    }
    var regex = /[aeiou]/gi;
    var vowelIndice = str.indexOf(str.match(regex)[0]);
    if (vowelIndice>=2)
    {
    consonant = str.substr(vowelIndice) + str.substr(0, vowelIndice) + ‘ay’;
    }
    
    return consonant;
    }
    
    translatePigLatin(“gloove”);
    

提交回复
热议问题