Intermediate Algorithm Scripting: Pig Latin

Intermediate Algorithm Scripting: Pig Latin
0

#1

Tell us what’s happening:
I’m having trouble iterating through the string until it reaches a vowel. How do you iterate a string until it stops at the first vowel? How do you get all of the first consonant letters before it reaches vowel? I iterate the string by using the for loop, and I use the “if” statement if the first letters are consonant, but I only get the first letter consonant.

Your code so far


function translatePigLatin(str) {
  var vowel = /[aeiou]/g;
  var cons = /[^aeiou]/g;
  console.log(cons.test(str[2]))
  var consonants = [];
  var index = str.charAt(0);

  console.log(str.slice(1, str.length))
  for(var i = 0; i < str.length; i++){
    console.log(str[i])
    if(cons.test(str[i])){
      consonants.push(str.charAt(i));
      console.log(consonants)
      var slice = str.slice(consonants.length, str.length);
      return slice + consonants + 'ay'
      
    }
  }
}

translatePigLatin("glove");

Your browser information:

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

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


#2

You are returning inside your loop. That means that as soon as your code gets to the end of the loop execution for the first time (after the first consonant), your function returns and is done.


#3

Do I put the return outside the if or the for loop?


#4

When I put the return outside the for loop I get “gloveway”, which it should return “oveglay”


#5

Oops. Nope. I get “gloveg,l,vay”, not “gloveway”. I get what you mean though.


#6

You’re on the right track. Now it’s a matter of figuring out what the state of all of your values are and why they don’t match your expectations.


#7

Okay, I think I might know how to solve this now. Thanks for the help. You gave me a huge tip. I’ll reply back if I have further questions.


#8

Happy coding!