Pig Latin - without vowels

Hey. Currently stuck on Pig Latin - “Should handle words without vowels”. My thought is that in case of word without vowel the function should return this word +ay. But it doesn’t pass the test. Can you please take a look at my code and tell what am I doing wrong?

function translatePigLatin(str) {
	let regex = /(^[^aouiey]{1,})([a-z]{1,})/;
	if (/[auieo]/.test(str[0])) {
		return str + 'way'
	} else if (/[auieoy]/g.test(str) === false) {
		return str + 'ay';
	} else {
		return str.replace(regex, '$2$1' + 'ay');
	}
}

translatePigLatin("cdzx"); //returns cdzxay

I also tried just returning the str as is if there is no vowel present but I didn’t pass the test also. I read wikipedia for the rules of pig latin and there is nothing stated there about how to handle cases of words without vowels.
any luck?

I got it to pass by making a clear if statement to handle this side case despite my code having already covered it.

function translatePigLatin(str) {
  if (str.search(/[aeiou]+/) == -1) {
    return str.concat('ay')
  }
  return /^[aeiou]/.test(str) ? str.concat('way') : str.replace(/^([^aeiou]+)(\w+)/,'$2$1ay');
}

It might be that the assertion tests look for search

1 Like

I’ve put “str.search” instead of “str.test” in my code and it worked, thanks. But I still don’t understand why it’s working with str.search and not with str.test. If anyone explains this to me I’d really appreciate it.
New code:

function translatePigLatin(str) {
  let regex = /(^[^aouiey]{1,})([a-z]{1,})/;
  if (/[auieo]/.test(str[0]) ) {
    return str+'way'
  }
  else if (str.search(/[aeiouy]/) === -1) { //changed line
    return str+'ay';
  }
  else {
    return str.replace(regex, '$2$1'+'ay');
    }
}

translatePigLatin("cdzx");


This really should be made explicit in the exercise notes, or at very least, a test case provided to show what the expected output should be. Very sloppy on fcc’s part here. I ended up copying the basic solution into my editor and throwing a few test cases of random all-consonant strings at it to see the results (the intermediate solution ended up throwing an error, oddly enough).

Third, I’ve got almost 2 decades of professional software development experience and these little algorithm solutions have required more regexp than I’ve ever done.