Fun with Pig Latin challenge

OK, so the challenge here was to create a Pig Latin translator/ Basically if the first letter or two are consonants, remove that and add - “AY” on the end. If the word begins with a vowel, then just tack “WAY” on the back end of the word and there you go.

Here is where I am :

function translatePigLatin(str) {
  str=str.toLowerCase();
  
  let c =(/[aeiou]/).exec(str);
 console.log(c.index)
  if(c.index=0){
    console.log(str)
    //str = str+"way";
   console.log (str);
  }  

    if(c.index=1){
    let r=str.substr(1);
    //console.log(r);
    let f= str.substr(0,1);
    str = r+f+"ay";
    console.log(str);
  } 
  console.log (c.index)
  if(c.index=2){
    let r= str.substr(2);
    let f= str.substr(0,2);
    str=r+f+"ay";
    return str;
 
    
  } 
  }


    
 
    
  
   // console.log(str.substr(i,1));
  
 
  

  
  
  
//  return str;


translatePigLatin("algorithm");

Not sure why this does not fly… … thank you in advance for your kind assistance.

Mark

It is not only the first letter or two, if your word start with a cluster of consonants you have to move all of them to the end:
strength -> engthstray. You also need to consider what happens if your word has no vowels. fly -> flyay, which your function doesn’t consider as you are matching vowels.

if(c.index=0){

This is not a comparison, the operator to compare is == or === (this happen every time you have an if statement)

Thanks Leahleen for the catch… I did miss the comparison operator… thats what I get for starting at 5 AM LOL

I had not actually gotten to the no vowel assumption, just yet… I just needed to figure out what was not working… I will tack that along the back end…

Thank you for the hand. !

Mark

Interestingly, as I look over the rules for the challenge, it is unclear which they want. either the “way” or the “ay” at the end of fly. This is because the rule in the challenge states
" Pig Latin takes the first consonant (or consonant cluster) of an English word, moves it to the end of the word and suffixes an “ay”. "

it is impossible to move a cluster of consonants to the “end” of the word if the thing you are checking for does not exist. neither does it begin with a consonant.

Here is the issue… Y incorrectly is assumed to be a consonant. It can be either…


So in the case of " FLY" it is a vowel… in the case of " YOLK" it is a consonant each dependent on its usage… In this view, the correct translation would be " yflay" Unless they include some other rule that says that if the Y is within a 3 letter word then that would not apply ?

Here again I have not looked at the “official resolution” as of yet… but its a tripping point.
Thank you again for your help… !

If is not actually an issue in this challenge, the y doesn’t seem to appear in the tests (or I don’t see it)

well that and the fact that my regex does not currently include the y anyway LOL… the “c.index” equivilent becomes “null” in this case.

So if the first vowel comes at the end… There is no rule in the challenge for that… AND they do not give an example of that… So for Ohio it would be Hiooway or Ohioay ? Or possibly for something like SKI the same rule would apply for the handling of the vowel preceded by a consonent cluster … and in either case… at least for me… this would be handled by the first group ? So they must be looking for something different all together :frowning: I dont think this was a well thought through challenge :frowning:

Ohio -> ohioway

Ski -> iskay

If the word start with a vowel there is the rule that say to add just way at the end

If the word start with a consonant you move the consonants at the beginning of the word to the end of the word + ay

If the word has only consonants just add ay

If the word has only vowels, it’s still valid the first rule I have listed first

Yes basically when I wrote my code it did not have specific code for the situation where the vowel comes at the end of the word, because by default, my code covered that scenario by finding the first occurence of ANY vowel. But the challenge still failed it on the basis that there was not SPECIFIC code for a vowel at the end of the word.

I did not have specific code for that case? It was included in the consonant cluster being moved

OK so here is the code ( with the added specific code for vowel at the end of the word… what am I missing here

function translatePigLatin(str) {
  str = str.toLowerCase();
console.log(str.length);
  let c = /[aeiou]/.exec(str);
  
    if (c === null) {
    console.log(str + "ay");
  } else if (c.index === 0) {
    str = str + "way";
    console.log(str);
  } else if (c.index === 1) {
    let r = str.substr(1);
    let f = str.substr(0, 1);
    str = r + f + "ay";
    console.log(str);
  } else if (c.index === 2) {
    let r = str.substr(2);
    let f = str.substr(0, 2);
    str = r + f + "ay";
    console.log(str);
  }else if (c.index = (str.length - 1)){
    str=str+"ay";
    return str;
  }

  // console.log(str.substr(i,1));

  return str;
}

translatePigLatin("ski");

OK nevermind… I hate when I post things… and then see where I screwed up uuuugh… nevermind LOL

THe Last Else if was changed to :

else if (c.index = (str.length - 1)){
    f=str.substr(0,(str.length-1));
    r=str.substr(str.length-1)
   str=r+f+"ay";
    console.log(str);
  }

and even though the test of “translatePigLatin(“kkkkkkkkkko”);” It passes in console… whats up with that ?

This isn’t really related to the solution.

  1. I would suggest you declare your variables at the top and only do assignments inside the ifs. I would also suggest not overwriting the parameter, make a new variable.

let l, r, newStr;

  1. You have an assignment operator in your last else if

else if (c.index = (str.length - 1))

@lasorg Right you are ! seems I have a bad habit of forgetting that little point about the assignment operator…

Anyone have a clue as to what is going on with this… its really bugging me and I kind of would like to move forward. current updated version :

function translatePigLatin(str) {
  str = str.toLowerCase();
console.log(str.length);
  let c = /[aeiou]/.exec(str);
  
   if (( /^[a-z].*[aeiou]$/igm).test(str)===true){
     f=str.substr(0,(str.length-1));
    r=str.substr(str.length-1)
   str=r+f+"ay";
    console.log(str);           

    }else 
  
    if (c === null) {
    console.log(str + "ay");
  } else if (c.index === 0) {
    str = str + "way";
    console.log(str);
  } else if (c.index === 1) {
    let r = str.substr(1);
    let f = str.substr(0, 1);
    str = r + f + "ay";
    console.log(str);
  } else if (c.index === 2) {
    let r = str.substr(2);
    let f = str.substr(0, 2);
    str = r + f + "ay";
    console.log(str);
  }else if (c.index = (str.length - 1)){
    f=str.substr(0,(str.length-1));
    r=str.substr(str.length-1)
   str=r+f+"ay";
    console.log(str);
  } 
   

  // console.log(str.substr(i,1));

  return str;
}

translatePigLatin("kkkkkkkkkko");
/*
translatePigLatin("california") should return "aliforniacay".
translatePigLatin("paragraphs") should return "aragraphspay".
translatePigLatin("glove") should return "oveglay".
translatePigLatin("algorithm") should return "algorithmway".
translatePigLatin("eight") should return "eightway".
Should handle words where the first vowel comes in the end of the word.
Should handle words without vowels.
*/

I don’t know f it is the only issue but here you are missing the change of str value

OK tried this version… it also worked in console… but not here :

let  regex=/aeiou/gi;
 var vowelIndice = str.indexOf(regex);
let f= (str.substr(vowelIndice));
let r =str.substr(0,str.length-1);

str=f+r+"ay";
return(str);

let regex = /aeiou/gi;
var vowelIndice = str.indexOf(regex);

If you console log vowelIndice what does it return and is that what you are expecting?

Verbalize the requirements, write down the steps you need to take in order to solve this problem. Break it down into small pieces. Test your assumptions about what your code is doing each step of the way. Don’t lose sight of important things like formatting and syntax, because if you do, you can have the solution to a logical problem but fail it because of syntax errors.

One way of trying to solve this problem would be to make an array of vowels and loop over the string and vowels array doing a compare character by character. It isn’t a very elegant or fancy approach but it might help you think more carefully about the problem.

Lasajorg, yes… the returned process correctly returns the string I am expecting… it just does not pass this test for some reason…

as with previous poster, I also put this code into Codepen to test… you can find it here…
isolated from my other code as well
https://codepen.io/BrBearOFS/pen/BvdoYQ?editors=1112