Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function translatePigLatin(str) {
- var strArr = [];
- var tmpChar;
- // check if the char is consonant using RegEx
- function isConsonant(char) {
- return !/[aeiou]/.test(char);
- }
- // return initial str + "way" if it starts with vowel
- // if not - convert str to array
- if (!isConsonant(str.charAt(0)))
- return str + "way";
- else
- strArr = str.split("");
- // push all consonats to the end of the array
- while (isConsonant(strArr[0])) {
- tmpChar = strArr.shift();
- strArr.push(tmpChar);
- }
- // convert array to string and concatenate "ay" at the end
- return strArr.join("")+"ay";
- }
- // test here
- translatePigLatin("consonant");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement