Challenge: Intermediate Algorithm Scripting: Pig Latin

Tell us what’s happening:

Challenge: Intermediate Algorithm Scripting: Pig Latin

I have tested all the testing words. The outcome are identical as they are supposed to be. But the system wouldn’t let me pass. Is there some latent mistake? Please help! Thank you soooo much!

[challenge description:
Translate the provided string to pig latin.
[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”.
If a word begins with a vowel you just add “way” to the end.
Input strings are guaranteed to be English words in all lowercase.]

Your code so far


function translatePigLatin(str) {
  let regex=/[aeiou]/;
  let a=str.match(regex);
  console.log("firstVowelIsLetter  "+a);

  let n=str.indexOf(a);
  console.log("indexOfVowelIs  n= "+n);

  let x=str.substr(n);
  console.log("firstVowelAndFollowings  x= "+x);
  console.log();

  if (n===0){console.log(str+="way"); return str+="way";}
  else if (n===-1){console.log(str+="ay"); return str+="ay";}
  else console.log(x+=str.substr(0,n)+"ay"); return x+=str.substr(0,n)+"ay";
}

translatePigLatin("california");

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36.

That’s because here:

console.log(x+=str.substr(0,n)+"ay");

you changed the value of x.
Add another console.log in else statement after the first one to see what happens.

1 Like

Ahhhhhhh Thank you soooo much!!! So basically is it a best practice to not include any algorithm in console.log()? It is better used for simply printing variables, debugging purposes, isn’t it?

Generally yes. But there are always exceptions and “bester” practices.

Actually some people consider these shorthand operations (+=, -= etc.) harmful and recommend to avoid them altogether.

But don’t take words like “best practices” and “antipatterns” as a gospel. Best practices and antipatterns change over time because at the end of the day they’re just opinions (that’s my opinion :smirk:).

1 Like

Got it! Thank you very much! I hope this post will help more people got the same error. You’ve helped them as well :slight_smile: