Pig Latin Challenge not working for 2 cases [CLOSED]

Pig Latin Challenge not working for 2 cases [CLOSED]
0

#1

Tell us what’s happening:
Hello Guys,

I am trying to figure out the code for Pig Latin challenge but I am not passing 2 cases.
Could anyone help me?

Thanks in advance and happy coding! :slight_smile:

Your code so far

function translatePigLatin(str) {

  var vowels = ['a','e','i','o','u'];
  var arr = str.split("");
  var strLength = str.length;

  for (var i = 0; i < strLength; i++){
    for (var j = 0; j < vowels.length; j++){
    	if (str[0] === vowels[j]){
          return str + "way";
        } else if(str[i]!==vowels[i]) {
          var firstIndex = str[i];
          arr.push(arr.shift(firstIndex));
          return arr.join("") + "ay";
        }
    }
  }
}

translatePigLatin("glove");
translatePigLatin("eight");

Link to the challenge:


#2

The way you have it set up, j only ever reaches A in vowels array, you return either way, breaking the loop.
If you move the else if out of the inner for loop and into the outer loop (and remove the if condition) this solves 1 of the 2 remaining problems.

this is just me trying to show the fixed code without changing how you fundamentally solved the problem

for (var i = 0; i < strLength; i++){
    for (var j = 0; j < vowels.length; j++){
    	if (str[0] === vowels[j]){ //inner loop now only looks for starting vowel
          return str + "way";
        } 
          var firstIndex = str[i]; // this runs only if the inner loop finds no vowel at str[i]
          arr.push(arr.shift(firstIndex));
          return arr.join("") + "ay";
        
    }

but as for the remaining problem, you basically need to take all the first consonants up to the first vowel. Currently you’re only taking off the ‘g’ from the front of glove not ‘gl’

you want something like this pseudocode to solve the whole challenge(you could use ifs or for loops for a problem like this)

if str[0] is vowel
  {
     return str + "way"
  }
else if(str[1] is vowel)
  {
     return str[1] through str[str.length-1] + str[0] +"ay"
  }
else if(str[2] is vowel)
  {
     return str[2] through str[str.length-1] + str[0]+str[1] +"ay"
  }

etc...

since its so few steps you could go with if statements or loops

Hope that helps
Good luck!


#3
function translatePigLatin(str) {
  function isVowelFirstLetter() {
    var vowels = ['a', 'e', 'i', 'o', 'u', 'y'];
    var vowelsLength = vowels.length;

    for (i = 0; i < vowels.length; i++) {
      if (vowels[i] === str[0]) {
        return true;
      }
    }
    return false;
  }

  if (isVowelFirstLetter()) {
    return str += 'way';
  }
  else {
    var consonants = '';
    while (isVowelFirstLetter() === false) {
      consonants += str.slice(0,1);
      str = str.slice(1);
    }
    str += consonants + 'ay';
  }
  return str;
}

translatePigLatin("glove");

#4

I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard.