Intermediate Algorithm Scripting: Pig Latin last requirement not working

Intermediate Algorithm Scripting: Pig Latin last requirement not working
0

#1

Hello, I have a problem passing the last test: Should handle words without vowels

function translatePigLatin(str) {
  let vowelRegEx = /[aeiou]/g;
  // in case we have no vowels just return string
  // This is working with string with no vovwels, but still not passing last test
  if(!str.match(vowelRegEx)) return str;



  // if string starts with a vowel append `way` at end
  if (str[0].match(vowelRegEx)) {
    str += 'way';
  // if str contains a vowel move first consonant (or consonant cluster)
  // to the end of the str and add `ay`
  } else if (str.match(vowelRegEx)) {
    str = str.replace(/([^aeiou]+)(\w*)/, '$2$1' + 'ay'); //?
  }
  return str;
}

console.log(translatePigLatin("rctsfg"))

#2

Where in the instructions does it tell you to just return the original str if there are no vowels?


#3

I have read the instructions again and you are right it does not say to just return the string. Here is the new code and it passes all the test cases. Thanks :slight_smile:

function translatePigLatin(str) {
  let vowelRegEx = /[aeiou]/g;

  // if string starts with a vowel append `way` at end
  if (str[0].match(vowelRegEx)) {
    str += 'way';
  // if str contains a vowel move first consonant (or consonant cluster)
  // to the end of the str and add `ay`
  } else if (str.match(vowelRegEx)) {
    str = str.replace(/([^aeiou]+)(\w*)/, '$2$1' + 'ay'); 
  } else {
  // if we have no vowels just add `ay` at end of string
    str = str + 'ay';
  }

  return str;
}