Need some help with regexpression

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:

str.match(mycheck) is an array

you may want to rethink your logic here

1 Like

The values you seem to be checking for aren’t the values string.match() returns. Take a look at the return values section on this page https://devdocs.io/javascript/global_objects/string/match

Thanks to you I was getting lost. so I rethought array and what is return ed as a value and added this logic and it works: The index will return -1(no vowel), 0 (first letter is vowel), greater that zero ( begins with a consanant).

if (str.indexOf(str.match(mycheck)) === 0||str.indexOf(str.match(mycheck)) === -1)

will just add the str plus the “ay”

thanks for the pointer

I used the else if loop works

if (str.indexOf(str.match(mycheck)) === 0) {

word = str + “way”;

} else if(str.indexOf(str.match(mycheck)) === -1){

  word = str + "ay";

} else {

Now you’re asking if this string contains the array. Again, you’re going to get unpredictable results.

Try looking at https://devdocs.io/javascript/global_objects/string/search - this takes the regex and gives you the index of the first match in the string.