Goat Latin LeetCode solution in JavaScript

1
/**
2
* @param {string} S
3
* @return {string}
4
*/
5
var toGoatLatin = function (S) {
6
let re = /[aeiou]/gi;
7
let word = S.split(" ");
8
let add = 0;
9
let arr = [];
10

11
for (let i = 0; i < word.length; i++) {
12
if (!word[i].substring(0, 1).match(re)) {
13
arr = word[i].split("");
14
let letter = arr.shift();
15
arr.push(letter);
16
word[i] = arr.join("");
17
}
18

19
// have to append 'ma' regardless if it starts with a vowel
20
word[i] += "ma";
21

22
// append 'a' number of times as the index + 1
23
add = i + 1;
24
while (add > 0) {
25
word[i] += "a";
26
add--;
27
}
28
}
29

30
return word.join(" ");
31
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0