I’m working on the Pig Latin algorithm and almost have it cracked but there’s a weird comma appearing on the ‘glove’ test case. It seems to be getting put there during the str.push(), but using str.join(’,’) doesn’t seem to get rid of it… Any insights?
// Pig latin translator
// Move first consonant cluster to the end, suffix -ay
// Suffix -way if vowel initial
function translatePigLatin(str) {
var vowels = ['a', 'e', 'i', 'o', 'u'];
str = str.split('');
var indices = [];
var fV = '';
// If it starts with a vowel
if (vowels.includes(str[0])) {
console.log('Starts with a vowel');
return str.join('') + 'way';
} else {
// If it doesn't start with a vowel
for (letter in vowels) {
if (str.indexOf(vowels[letter]) != -1) {
// Get the indices of the vowels
indices.push(str.indexOf(vowels[letter]));
fV = Math.min(...indices);
}
}
}
str.push(str.slice(0, fV));
str.splice(0, fV);
return str.join('')+'ay'; // Outputting 'oveg,lay'
}
translatePigLatin('glove'); // 'oveglay'