Two questions about my approach to Pig Latin

Tell us what’s happening:
I need a little bit of a shove in the right direction. I’m not passing any tests, but I feel pretty good about the general approach I’m taking. I’m concerned that I may not be using splice() correctly or may be misunderstanding what I’m producing and this misunderstanding is affecting the rest of my code.
So, below, I want to say what I think is happening and please correct my logic where it is faulty.

  1. I captured the first character and remaining str to convert the string to an array.
  2. I’m running that now altered form of str through a regex that searches for an instance of consonant or vowel on newStr, adding the first character to the end of the remaining string in endStr.

My idea is that I use newStr too much and that this is running me into some problems. But it doesn’t seem this should be a problem because the first time I use it is in a method that use regex to determine what kind of letter the first character is in order to determine the suffix that should be added to it.

Your code so far


function translatePigLatin(str) {
var newStr = str.slice(0);
var endStr = str.slice(1);
  var strTwo = endStr + newStr.split(/^[aeiou]+$/, + newStr + "way" || /^[^aeiou]+$/, + newStr + "ay");
  console.log(strTwo);
  return strTwo;
}

translatePigLatin("consonant");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36.

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

what about a word like cream? or through?

How are these any different than “consonant” (the word I’m using now)?

okay, let’s translate these three words into pig latin:
consonant => onsonantcay
cream => eamcray
through => oughthray

Will all three of these work with your approach?

You might want to read again the description of how Pig Latin works.

Thanks for the reminder. I completely overlooked that.
I’m still stuck on a single aspect of the problem and I’m wondering if the fault is with my syntax or my understanding of how capturing groups actually work.

One thing I’m concerned about is that my captured groups don’t accurately work with the entire string; rather, work only with the portion of the string that has a consonant cluster followed by a vowel (and therefore the remaining str thereafter is ignored). Am I correct in assuming this? Is this my problem? Or do I need to look elsewhere?
Oh wait, I just realized that /.*/ means everything. So I think this answers some of my questions above, but I’m still not sure where I’m going wrong.


function translatePigLatin(str) {
    //if there is a str beginning with a vowel, then return that string with "way" added at the end
    if(/^[aeiou]/i.test(str)){
return str + "way";
    } else {
        //if there is a str beginning with one or more non-vowels followed by a vowel, then replace str arrangement with non-vowel beginning at end of str and add "ay" to non-vowel beginning at end of string.
        return str.replace(/^.([^aeiou]+)([aeiou].*)$/, "$2$1ay");
    }
}

translatePigLatin("algorithm");

Okay. I took . out at the beginning of the second regex and passed most of the cases, but I’m still missing the last one concerning vowel-less words.

Although it didn’t work, I tried another else statement with a replace() and using slice method to capture the first letter and remaining remaining letters and then use capturing groups to arrange them properly. This didn’t work but it seems that it should and I wonder if I’m onto the right solution but not the correct syntax? An example of what I’m talking about is below.

else {
return str.replace((str.slice(0,1)), (str.slice(1)), "$2$1ay";
}

I solved the challenge, but I have some questions in regard to it.

I noticed that a lot of people used various array methods along with for loops for solving this. I thought that using as few methods as possible and perhaps only an if statement and regex would mean less data used (speaking hypothetically, as I know the challenge was intended to be flexible to various approaches).

But I’m interested in this:
if regex, when working in read-world scenarios, would be preferred to some of the other approaches I mentioned above because it really provides a concise explanation of what to look for…or if for loops and methods used would be about equal to data used? Perhaps they would use even less?

Just interested. I’m getting into Big O and thinking about it more and more.

Thank you for any feedback.


[spoiler]function translatePigLatin(str) {
   /*if there are only consonants*/
   if(!str.match(/[aeiou]/)){
   return str.concat("ay");
   } 
   /*if if begins with a vowel*/    
   if(/^[aeiou]/i.test(str)){
   return str+ "way";
   }
   else{
       /*if there is a str beginning with one or more non-vowels followed by a vowel, then replace str arrangement with non-vowel beginning at end of str and add "ay" to non-vowel beginning at end of string.*/
    return str.replace(/^([^aeiou]+)([aeiou].*)$/, "$2$1ay");
}
//if(/^[aeiou]/i.test(str)){
 //  return str + "way";
//}


}

translatePigLatin("my");[/spoiler]

1 Like