Tell us what’s happening:
With this code the matching is working for all the test scenarios with the exception of the word rhythm. the result is the ay is displayed. I have change the expression to look for first position of vowels, then global flag on the vowels.
Your code so far
function translatePigLatin(str) {
let word = '';
var mycheck = /^[aeiou]/gi;
console.log(str.match(mycheck));
if (str.match(mycheck)=== -1||str.match(mycheck)=== 0) {
word = str + "way";
//} else if (str.indexOf(str.match(mycheck)) === 0) {
// word = str + "way";
} else {
// Find how many consonants before the first vowel.
//let vowelPos = str.indexOf(str.match(mycheck)[0]);
let vowelPos = str.indexOf(str.match(/[aeiou]/));//index of first vowel
console.log(" pos = " + vowelPos);
// Take the string from the first vowel to the last char
// then add the consonants that were previously omitted and add the ending.
console.log(str.indexOf(str.match(/[aeiou]/)));
word = str.substr(vowelPos) + str.substr(0, vowelPos) + "ay";
}
console.log(word);
return word;
}
translatePigLatin("rhythm");
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36
.
Challenge: Pig Latin
Link to the challenge: