Pig Latin Translator - Conditonal Issues

Tell us what’s happening:
I think I pretty close to solving this one. The loop does iterate through the string I split into an array, but the if statement’s condition doesn’t catch any other consonants it encounters except for the first one. I also can’t get this to work for strings that don’t have any vowels. I’ve spent the last 2 days trying a number of methods to get this working.

Your code so far


function translatePigLatin(str) {
let testStr = str.split("");
let testRegex = /^[aeiou]/i;

  if (testRegex.test(str)) {
    return str.concat("way");
  } else if (!testRegex.test(str)) {
    for (let i = 0; i < testStr.length; i++) {
      if (testStr !== testRegex) {
        testStr.push(testStr.shift());
        return testStr.join("").concat("ay");
      }
    }
  }
}

console.log(translatePigLatin("glove"));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0.

Challenge: Pig Latin

Link to the challenge:

What are you trying to accomplish in the for loop?

You are testing that an array is not strictly equal to a regular expression object. That comparison will always be true, since they are different types. That is why you currently are not getting past the first consonant.

What I was trying to accomplish in the loop was to have each letter of the string tested for a consonant. If it does evaluate to true, shift() and push() those consonants to the end and to stop when it reaches a vowel.

What do you mean when you say they are different types? Is it because an regular expression is trying to compare itself to an array and not a string?

Here you are comparing a string to a regular expression. They are not comparable this way.

You always return if the first letter is not equivalent to the regular expression. That’s why you are only checking the first letter.

What you want to implement then is using the .test() method of the regular expression to test the characters.

Something along the lines of if (!testRegEx.test(testStr[i]) for the comparison is what you want to do.

There is more logic that you will need to work out, I do suggest using console.log() to look at variables while you are running your code to better understand what is happening.

1 Like