Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. function translatePigLatin(str) {
  2. var strArr = [];
  3. var tmpChar;
  4.  
  5. // check if the char is consonant using RegEx
  6. function isConsonant(char) {
  7. return !/[aeiou]/.test(char);
  8. }
  9.  
  10. // return initial str + "way" if it starts with vowel
  11. // if not - convert str to array
  12. if (!isConsonant(str.charAt(0)))
  13. return str + "way";
  14. else
  15. strArr = str.split("");
  16.  
  17. // push all consonats to the end of the array
  18. while (isConsonant(strArr[0])) {
  19. tmpChar = strArr.shift();
  20. strArr.push(tmpChar);
  21. }
  22. // convert array to string and concatenate "ay" at the end
  23. return strArr.join("")+"ay";
  24. }
  25.  
  26. // test here
  27. translatePigLatin("consonant");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement