Pig latin loop assistance

hey guys, so I’ve been this on and off for days.

so my question is this.

I want to replace the 1st character of the string which isn’t a vowel const replacement = str.replace(str[i], '');
but how come it doesn’t stop? why does it continue to loop to the second character even if it is a vowel? thanks.

  **Your code so far**

function translatePigLatin(str) {
const vowels = 'aeiou'
// loop string
for (let i = 0; i < str.length; i++) {
  // loop vowels
  for (let j = 0; j < vowels.length; j++) {
    // condition if string starts with a vowel
    if (str.startsWith(vowels[j])) {
      return str + 'way';
    }
    // condition if string doesn't start with a vowel
    else {
      const replacement = str.replace(str[i], '');
      console.log(replacement)
    }
  }

}
}

translatePigLatin("california");

/*
loop string
check if string begins with a vowel
if it does 'way' to the end
if it doesn't remove non vowel letters, add them to the end of the string and add 'ay' to the string.
return that string
*/
  **Your browser information:**

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

Challenge: Pig Latin

Link to the challenge:

Double check your logic here. Your loop over the vowels is inside of the loop over every single letter. But you have an if else inside of that loop. Every time the if condition doesn’t trigger, the else condition will instead. That replacement condition isn’t really what you want in that location. Also, it changes your original string in ways that will create surprising results. Your letters will shift, changing their indices.

Is there anywhere I can learn about the topic clearer? Just by reading your explaination at first sight it is still confusing.

Hmm, I was a bit off with the behavior of replace in my mind, but it’s still not where you want in to be. Also, shouldn’t that letter go somewhere instead of being deleted?

In words, your first iteration of the algorithm says

  1. look at the first letter of my word

  2. loop at the first vowel

  3. if the first letter of the word is a then return the word with ‘way’ at the end

  4. if not, remove the first letter

Now, you aren’t currently doing anything with the string where you removed the first letter, but this logic won’t work for ‘orange’. That starts with a vowel, but that vowel is after a, so your if statement would have removed the o in orange after finding that it isn’t an a.

question why is the first word is ‘a’ , shouldn’t it be one of the vowels since i loop through them?

for my understanding I first loop through the vowels and then I check the condition or am I wrong?

Your if statement is inside of the loop. That means that every single time the if statement is not met inside of the loop, the else condition is triggered.

But still shouldn’t it be every single time it is not vowels[j] which means either one of the aeiou? letters

No. In words, your code says, ‘if the string does not start with vowel j, then remove the first letter of the string’. Every single time the if condition is not met, the else condition will execute. Every single time. This is inside of a loop over all vowels, so every single vowel that doesn’t match will cause the else statement to execute.

So you are telling me for the word orange, it is not going to loop until the letter o, but see that a is wrong hence jump to the else statment?

Yup. That’s how an if-else works.

It seems very weird, shouldnt it loop to see that none of the vowels match first?

The if else literally says

if (this condition is true) {
  then run this code
} else {
  run this other code
}

The code in the if block always runs when the if condition is true.

The code in the else block always runs when the if condition is false

But I thought it should at least first check all the vowels[j] first before jumping to the statement thats why i wrote vowels[j] in the if statement…

It only checks for the current value of j. The entire loop body runs for every value of j.

And then if it sees 1 is wrong it jumps to the next statement it wont check the rest…

Do you have a hint for me how I can implement my logic then? I feel lost.

Dont tell me yet, when i get home ill try to think it over first, thanks.

Small hint: this issue very similar to part of the logic in the challenge where you were comparing a list of objects to a target object.

yes it did remind me of my solution in Wherefore art thou

function translatePigLatin(str) {
  const cluster = /^[^aeiou]+/gi;
  const vowels = 'aeiou'
  // loop string
  for (let i = 0; i < str.length; i++) {
    const vowelStart = false;
    // loop vowels
    for (let j = 0; j < vowels.length; j++) {
      // condition if string starts with a vowel
      if (str.startsWith(vowels[j])) {
        vowelStart = true;
      }
    }
    if (vowelStart) {
      return str + 'way'
    }
    else if (str.charAt(0).indexOf(vowels === -1)) {
      const subCluster = str.replace(cluster, '');
      const returnedCluster = str.match(cluster).join();
      return subCluster + returnedCluster + 'ay';

    }
  }
}

translatePigLatin("glove");

/*
loop string
check if string begins with a vowel
if it does 'way' to the end
if it doesn't remove non vowel letters, add them to the end of the string and add 'ay' to the string.
return that string
*/

I have made it work I am really happy!, could have I made it work with my previous logic only using slice? I couldn’t so that’s why I had used something else.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.