Pig Latin code explanation

Hello everyone, I’m currently at the Pig Latin challenge and I manage to pass every test except the “no vowels” one. After taking a look at the code provided by FCC, I came across one line I’m having trouble understanding!

// Find how many consonants before the first vowel.
var vowelIndice = str.indexOf(str.match(regex)[0]);

Could someone explain to me exactly what this line does and especially that “[0]” at the end.
Thanks in advance;

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/pig-latin/

Link to the basic solution provided by FCC:
http://forum.freecodecamp.org/t/freecodecamp-algorithm-challenge-guide-pig-latin/16039

1 Like

What this line does is:

  • str.match(regex) // returns an array with every regex match
  • str.match(regex)[0] // grab only the first match (the first element of the array)
  • str.indexOf(str.match(regex)[0]) // in our str, what is the index of the first matched character

Still, none of the FCC provided codes works for me. Someone probably should do a pull request to fix those codes.

If you want to pass the test no vowels, make sure your code handles the word rhythm by transforming it into rhythmay.

2 Likes

Thanks a lot for the explanation friend. I think the reason i can’t pass the all vowel test is because I treat “y” as a vowel too. I’ll change it and try again!
Cheers