Fun with Pig Latin challenge

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

When I ran some tests using that codepen it’s not working as intended.

“california” returns “acaliforniay” when it should return “aliforniacay”
“glove” returns “eglovay” when it should return “oveglay”

so right now it doesn’t remove the consonant from the beginning and moves them to the end.

“gl” should be removed from “glove” and moved to the last indexes giving you “ovegl” then you append “ay” resulting in “oveglay”.

That particular codepen was only meant to test the ONE scenario when the vowel comes at the end of the word. Now, if you take the code this one…

PigLatin Translator Link
That will provide the correct answer to the test your looking to solve…

That particular codepen was only meant to test the ONE scenario when the vowel comes at the end of the word. Now, if you take the code this one…

PigLatin Translator Link
That will provide the correct answer to the test your looking to solve…

That one still gives me the wrong answer,
“california” → “acaliforniay”

You need to remove any consonants from the beginning and append them to the end.
So for california that would mean only c gets moved to the back since the next char is a vowel.
After that is done, append “ay”.

if it was glove then you would remove gl and stop at o since o is a vowel.
Returning “oveglay”

Sorry… I put in some extra code while monkeying around with it… I commented out that part and it now functions correctly…

Yes, now it works :slight_smile:

Right now it’s coded with variables having single letter names, for someone who hasn’t written that code it’s very hard to work with and understanding what’s going on.
I would suggest refactoring the code as practise and also use variables which makes more sense.

Agreed Ronnehag. This code needs to be rewritten without so much in the way of loop specificity for variables… etc. I was attempting to work in a microscopic way due to attempting to understand the test. Considering the flow of " make it work… make it pretty… make it fast" I just want it to work at this point … It works… the requirement has been fulfilled but it does not pass the test… which is ultimately the issue I am trying to understand.

Which part of the test doesnt it pass?

It does not pass the question if the first vowel comes at the end of the work… ie THE

Just to help with getting rid of duplicate declarations /refactoring etc… Here is a cleaner version which also does the correct responses :slight_smile: ( Swap the console.log statements for return statements its a thing with Codepen )

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

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

  console.log(str);
}

translatePigLatin("california");

small issue… in testing this version I have a " can not read property ‘index’ of null…
more work to do there -

I get that this is something where I need to insert some form of error trapping code for this… and as a silly side note the following code now passes all the tests EXCEPT the “word has NO vowels.” go figure… :slight_smile:

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

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

 return str;
}


translatePigLatin("consonant");

In addition - I put in this part of the code :

  console.log(str)
  let c = /[aeiou]/.exec(str);
  console.log (c)

str = "qqq"

which produces :
“qqq”
null "

Just for the fun of it… I copied/pasted their solution from the guide in the forum for this challenge… and it des not pass the test for the Vowel at the end of the word either ROFL …

It works just fine for me, you are missing the if else part of the solution (i assume you are talking about the “Basic Code Solution” you get when you press the “Get a hint” button)

else if(str.match(regex) === null)

NO I didnt go there. I went to the Forums and in search field type in Pig Latin and the “Guide” there provides several answers. I took the “Basic” version… copied and pasted it in and tested…

That aside… my original issue still persists … in its current state - it will pass everything except if there are no vowels at all in the word… the test it fails is labeled as : Should handle words without vowels. - and it is correct it does not… but the question that there is something that is syntactically wrong with the code I get that… I just have no idea what THAT is… `function translatePigLatin(str) {
str = str.toLowerCase();
console.log(str)
let c = /[aeiou]/.exec(str);
console.log (str.indexOf©)
let r = str.substr(c.index);
let f = str.substr(0,(c.index));
//console.log©;

if(str.match(c) === null) {
   str=str+"ay";

console.log(str);
} else if (c.index === 0) {
str = str + “way”;
console.log(str);

} else if(c.index >=1){

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

}

console.log(str);
}

translatePigLatin(“qrs”);`

As a side note… the reference to the indexOf - in there I also tested using something like
" if (str.indexOf©=-1 {}
That also did not produce the desired result… :frowning:

The assignment must have been updated at some point because none of the solutions in that thread works with the current requirement of “Should handle words without vowels”. The get a hint guide (<-- has spoilers) does however work.

Can you edit and reformat your code in your last post it hard to see what is going on.

THis seems to be the fly in the ointment :

 else 
   if (str.match(d) === null) {
    nstr = str + "ay";
     
    console.log("dud");
  }

I can’t see any difference between the “accepted answer and mine”…

Here is the entire code :

function translatePigLatin(str) {
  str = str.toLowerCase();
  let d = /[aeiou]/gi;
  let nstr = "";

  let vindex = str.indexOf(str.match(d)[0]);

  console.log(str.match(d)); 
 
  if (str[0].match(d)) {
    nstr=str+"way"
    console.log(nstr);
    
  } else 
   if (str.match(d) === null) {
    nstr = str + "ay";
     
    console.log("dud");
  }else {
    //let index=(str.indexOf(str.match(d)));
    nstr = str.substr(vindex) + str.substr(0, vindex) + "ay";

    console.log(nstr);
  }
 return nstr;
  }

translatePigLatin("consonant");

When your function is passed a string that does not contain a vowel it will crash on this line (Cannot read property ‘0’ of null).

let vindex = str.indexOf(str.match(d)[0]);

So it will never reach the else if statment

  1. Declare vindex with your other variable up top.
  2. Wrap the line in an if check and test str.match(d)
  3. Only reassign to vindex inside this new if, don’t redeclare it.

Thanks for trying to help… I greatly appreciate that !

  1. No i just mean declare it with your other variables, like below the nstr variable.
let nstr = "";
let vindex;
  1. When your function is passed a string without a vowel str.match(d) will return null. The line where you assign a value to vindex should only run when str.match(d) does not return null (null is a falsey value).

if ( str.match(d) ) { ...code that only runs when str.match(d) does _not_ return null }

  1. I just mean after you have declared vindex with your other variables remember to not redeclare and only assign to it. I’m sure you know this already, it was just a friendly reminder.

vindex = str.indexOf(str.match(d)[0]);

1 Like

Got … Thank you for wading through that with me… ! I just could not wrap the brain around the whole property 0 of Null thing… or why exactly it was that I could not get the negation of that to work…

Anyway… it is now sorted and passes… On to the next round !

{_P CHeers !

1 Like