JavaScript: Pig Latin

Tell us what’s happening:

I don’t understand what I’m doing wrong. The code returns what’s expected in the console.

Your code so far


function translatePigLatin(str) {
  let vowelReg = /^[aeiou]/;
  let noVowelsReg = /[^aeiou]/;
  let allConsonantsReg = /^[^aeiou]+[aeiou]$/;
  let twoConsonantsReg = /^[^aeiou]{2}/;
  let endVowelReg = /[aieou]$/;

  console.log("Before modification: " + str);
  console.log("Vowel beginning: " + vowelReg.test(str));
  console.log("End with vowel: " + endVowelReg.test(str));
  console.log("Two consonoants: " + twoConsonantsReg.test(str));
  console.log("Yet another test: " + allConsonantsReg.test(str));

  if(vowelReg.test(str)) {
    str = str + "way";
    console.log("Test One");
  } else if(!(twoConsonantsReg.test(str))) {
    let num = 1;
    console.log("Test Two");
    str = str.substring(num) + str.substring(0, num) + "ay";
  } else if (endVowelReg.test(str[str.length - 1]) && 
             twoConsonantsReg.test(str) && 
             !(allConsonantsReg.test(str))) {
    let num = 2;
    str = str.substring(num) + str.substring(0, num) + "ay";
    console.log("Test Three");
  } else if (allConsonantsReg.test(str)){
    let num = 1;
    str = str.substring(num) + str.substring(0, num) + "ay";
    console.log("Test Four");
  } else {
    let num = 1;
    str = str.substring(num) + str.substring(0, num) + "ay";
  }
  console.log("Result: " + str);
  console.log();
  return str;
}
  
var noVowelsReg = /[^aeiou]/;

let test = "rhythm";
console.log("No Vowels: " + noVowelsReg.test(test));

translatePigLatin("consonant");
translatePigLatin("algorithm");
translatePigLatin("glove");
translatePigLatin("california");
translatePigLatin("bye");
translatePigLatin("rhythm");

Your browser information:

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

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

Your noVowelsReg is not able to exclude words with vowels, maybe you want to review again how to match exactly a string. twoConsonatsReg will match also those that beginning with more than two consonants but will treat them as if they have only two consonants at the start
Your last two tests don’t have the correct output in the console.
I have not marked all the issues, but here all your logged output, with a few corrections (There are some things that I have not marked and are still wrong)

(This is about "rhythm")
No Vowels: true

Before modification: consonant
Vowel beginning: false
End with vowel: false
Two consonoants: false
Yet another test: false
Test Two
Result: onsonantcay

Before modification: algorithm
Vowel beginning: true
End with vowel: false
Two consonoants: false
Yet another test: false
Test One
Result: algorithmway

Before modification: glove
Vowel beginning: false
End with vowel: true
Two consonoants: true
Yet another test: false
Test Three
Result: oveglay

Before modification: california
Vowel beginning: false
End with vowel: true
Two consonoants: false
Yet another test: false
Test Two
Result: aliforniacay

Before modification: bye
Vowel beginning: false
End with vowel: true
Two consonoants: true
Yet another test: true
Test Four
Result: yebay // should be ebyay

Before modification: rhythm
Vowel beginning: false
End with vowel: false
Two consonoants: true
Yet another test: false
Result: hythmray // should be rhythmay

I was meaning to comment that out after I posted this. That does nothing.

Alright I’m gonna have to do some explaining here. The Result is what is returned from the function. The rest of the tests are for testing out my regex to determine my if condition. As you can see all of them have different values for the different tests. The Result is still what matters here. And that checks out all of the requirements for the challenge.

Is it to shift it 2 positions?

And then keep it as is…

I changed the last one. But the first one, I’ve got it running as you commented it out and it still won’t pass.

Idk the instructions for this challenge are extremely vague.

You need to move the consonant cluster at the beginning of the word to the end of the word and add ay
bye, consonant cluster is by so it becomes eby and then you add ay so ebyay
rhythm, consonant cluster is the whole word, so you just need to add ay at the end: rhythmay. noVowelReg should say true and you just add ay at the end`

I did that tho. It is ebyay now. Still not passing. Here’s the changed line of code.

else if (allConsonantsReg.test(str) && noVowelsReg.test(str)){
    let num = 1;
    str = str.substring(num) + str.substring(0, num) + "ay";
    console.log("Test Four");

The values of the regex are in my initial code.

let noVowelsReg = /([aeiou]+$)/;

let noVowelsReg = /([aeiou]+$)/
This will match any word that ends with one or more vowels… you need something different for a regex that matches a word with no vowels.

function translatePigLatin(str) {
  let vowelAtFirstReg = /^[aeiou]/;
  let allVowelsReg = /[aeiou]/g;
  let clusteredConsonatsReg = /^[^aeiou]{2,}/;

  console.log("Before modification: " + str);

  let counterArr = [];
  while(allVowelsReg.test(str) == true) {
    counterArr.push(1);
  }

  console.log(counterArr);
  console.log("Only one last vowel: " + (counterArr.length == 1));

  if(vowelAtFirstReg.test(str)) {
    str = str + "way";
    console.log("Test One");
  } else if(!(clusteredConsonatsReg.test(str))) {
    let num = 1;
    console.log("Test Two");
    str = str.substring(num) + str.substring(0, num) + "ay";
  } else if (clusteredConsonatsReg.test(str) &&
             allVowelsReg.test(str) &&
             !(counterArr.length == 1)) {
    let num = 2;
    str = str.substring(num) + str.substring(0, num) + "ay";
    console.log("Test Three");
  } else {
    if(["a", "e", "i", "o", "u"].indexOf(str) > 1) {
      // Attempt for the second to last task
      console.log("Test Four");
    }
    else if (counterArr.length == 1){
      let num = str.length - 1;
      str = str.substring(num) + str.substring(0, num) + "ay";
      console.log("Test Five");
    } else {
      str = str + "ay";
      console.log("Test Final");
    }
  }

  console.log("Result: " + str);
  console.log();
  return str;
}
  
translatePigLatin("shwartz");
translatePigLatin("bllla");
translatePigLatin("the");
translatePigLatin("cbe");
translatePigLatin("bye");
translatePigLatin("rhythm");
translatePigLatin("consonant");
translatePigLatin("algorithm");
translatePigLatin("glove");
translatePigLatin("california");
translatePigLatin("cbe");
translatePigLatin("bye");
translatePigLatin("byee");
translatePigLatin("byeee");
translatePigLatin("byeeee");
translatePigLatin("bore");
translatePigLatin("eight");

Still not passing.

function translatePigLatin(str) {
  let vowelAtFirstReg = /^[aeiou]/;
  let allVowelsReg = /[aeiou]/g;
  let clusteredConsonatsReg = /^[^aeiou]{2,}/;

  console.log("Before modification: " + str);

  let counterArr = [];
  while(allVowelsReg.test(str) == true) {
    counterArr.push(1);
  }

  console.log(str.search(allVowelsReg));
  console.log(counterArr);
  console.log("Only one last vowel: " + (counterArr.length == 1));


  if(vowelAtFirstReg.test(str)) {
    str = str + "way";
    console.log("Test One");
  } else if(!(clusteredConsonatsReg.test(str))) {
    let num = 1;
    console.log("Test Two");
    str = str.substring(num) + str.substring(0, num) + "ay";
  } else if (clusteredConsonatsReg.test(str) &&
             allVowelsReg.test(str) &&
             !(counterArr.length == 1)) {
    let num = 2;
    str = str.substring(num) + str.substring(0, num) + "ay";
    console.log("Test Three");
  } else {
    if(str.search(allVowelsReg) > 1) {
      // Attempt for the second to last task
      let num = str.search(allVowelsReg);
      str = str.substring(num) + str.substring(0, num) + "ay";
      console.log("Test Four");
    }
    else if (counterArr.length == 1){
      let num = str.length - 1;
      str = str.substring(num) + str.substring(0, num) + "ay";
      console.log("Test Five");
    } else {
      str = str + "ay";
      console.log("Test Final");
    }
  }

  console.log("Result: " + str);
  console.log();
  return str;
}
  
translatePigLatin("shwartz");
translatePigLatin("bllla");
translatePigLatin("the");
translatePigLatin("cbe");
translatePigLatin("bye");
translatePigLatin("rhythm");
translatePigLatin("consonant");
translatePigLatin("algorithm");
translatePigLatin("glove");
translatePigLatin("california");
translatePigLatin("cbe");
translatePigLatin("bye");
translatePigLatin("byee");
translatePigLatin("byeee");
translatePigLatin("byeeee");
translatePigLatin("bore");
translatePigLatin("eight");

Finally I got it working using the search() method. If I implement it on all the other test cases the code will be much shorter for sure. Passing it is what counts. The instructions need a bit more clarification though.