Goat Latin LeetCode solution in JavaScript
1/**2* @param {string} S3* @return {string}4*/5var toGoatLatin = function (S) {6let re = /[aeiou]/gi;7let word = S.split(" ");8let add = 0;9let arr = [];1011for (let i = 0; i < word.length; i++) {12if (!word[i].substring(0, 1).match(re)) {13arr = word[i].split("");14let letter = arr.shift();15arr.push(letter);16word[i] = arr.join("");17}1819// have to append 'ma' regardless if it starts with a vowel20word[i] += "ma";2122// append 'a' number of times as the index + 123add = i + 1;24while (add > 0) {25word[i] += "a";26add--;27}28}2930return word.join(" ");31};