Stuck using String.match() for regex

Tell us what’s happening:
As far as I can tell, I have the regex pattern defined so that it looks at the beginning of the string for any match. However when a string such as algorithm is passed in, I get an error: TypeError: str.match(...) is null. Since null is falsey, I would have thought that the second return statement would have been returned but it’s not. How can I fix this so that any words that start with a vowel return {str} + “way”?

Your code so far


function translatePigLatin(str) {
var match = str.match(/^[^aeiou]+/i)[0];
if (match) {
  return str.slice(match.length) + str.slice(0, match.length) + "ay";
}
return str + "way";
}

translatePigLatin("algorithm");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0.

Challenge: Pig Latin

Link to the challenge:

The regex you have is looking for a string at the beginning that contains no vowels. For “algorithm”, it looks and sees an immediate vowel so it says that it found nothing. If you had the string “strong”, it would match for “str”.

I would recommend playing around with something like regex101.com, to test out difference patterns.

Regex is weird and confusing, but it also amazingly powerful.