translatePigLatin problem with some some words for testing case

Hello,
any idea in this code about of how could to resolve the problem for the case test?

translatePigLatin(“glove”) ; translatePigLatin(“schwartz”) ; translatePigLatin(“rhythm”);

any suggestion is appreciated :slight_smile:

 translatePigLatin(str) {
     let regex = /^[aeiou]{1}/
   if(regex.test(str)){

        str = str.replace(/.{0}$/, 'way')
  }else{
        str = str.slice(0, 10) + str[0] + str.slice(10);
        str=str.replace(str[0],'')
        str = str.replace(/.{0}$/, 'ay')
  }
  return str;
}

translatePigLatin("rhythm");
function translatePigLatin(str) {
  let regex = /^[aeiou]{1}/ , text;

if(regex.test(str)){
   text = str.replace(/.{0}$/, 'way')
  } else {
      //get the first consonant or the group of consonants before first vowel
      //if any
      let nonWowelGroup = str.match(/[^aeiou]+/)[0];
      //concatenate the text after the first consonant or the group to the  group and suffix "ay"
      text = str.slice(nonWowelGroup.length) + nonWowelGroup + "ay";  
  };
  return text;
}

translatePigLatin("california");

@alb0084, I literally have doubts about every line of your code. I suggest you start with easier problems and try this one a bit later.

Little hint for you:
If you want to add one string to another (concatenate), you can simply:

const a = 'a';
const b = 'way';
const concat = a + b;

@camperextraordinaire in my own solution before to posted this once in regex pattern i used this pattern matching -> ^(a|e|i|o|u){1} this seems to not work in my javascript code indeed when i try to call function test on my function test, the function test() return always true,whilst this ^[aeiou]{1} clearly work…could you tell me why may this happen?

@camperextraordinaire sorry i do not pay attention for spacing but actually without the space does not work anyway…or better the function test() of regex’s object return always true ( but for some strange motive in online regex editor this go well).

-> here there link of my test…https://jsbin.com/woxewix/edit?js,console

@camperextraordinaire here my test code… is what i said previously:

function functionTest(str) {
var word = str ;
var re = new RegExp(“(a|e|i|o|u){1}”);
console.log(re.test(word));
return str;
}
functionTest(“nnnnojjj”);

here “nnnnojjj” the matchin pattern regex would be returned false because in the first index of my string there is not any Vowel…how come it returns anyway true?
here there is the link about my test.

All clear…thanks a lot @camperextraordinaire .
Cheers

sorry @GeorgeCrisan what difference between /^[aeiou]{1}/ and /[^aeiou]{1}/?

What if you do:

let match = (str.match(/^[^aeiou]+/) || [''])[0];
return str.slice(match.length) + match + (match.length ? 'ay' : 'way');

There’s one even sexier solution with .replace()
:slight_smile:

@snigo you are too high skilled mate!..before to think that solution at least it me takes 100 years. :rofl: