Pig Latin All tests are passed. Except "Should handle words without vowels."

Pig Latin All tests are passed. Except "Should handle words without vowels."
0

#1

Tell us what’s happening:
All tests are passed. Except “Should handle words without vowels.” Please help me to get through this.

Your code so far


function translatePigLatin(str) {
  let startChar = str[0];
  let vowelChar = /[^aeiou]/;
  var firstVowelIndex;
  let newStr = "";
  firstVowelIndex = str.indexOf(str.match(/[aeiou]/g)[0]);
    if(firstVowelIndex >0){
  //if(vowelChar.test(str[0])){   
    newStr = str.slice(firstVowelIndex) + str.slice(0,firstVowelIndex)+ "ay";
  }
  if(firstVowelIndex ==0) {
    newStr = str + "way";
  }
  if(firstVowelIndex < 0) {
    newStr = str + "ay";
  }
   return newStr;
}
translatePigLatin("consonant");

Your browser information:

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

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


#2

Make sure your code returns the origal string unchanged with ay at the end. So “my” becomes “myay"

(I think calling match will give a null and you are trying to find the index of null. Instead check what match gave you first before doing the indexof part)


#3

Thank you @hbar1st :slight_smile: It would be a lot easier if it (about string unchanged with “ay” at end) is mentioned in the exercise.

Yes, match gives a null, so I tried in a different way. Followed your suggestions.
Thanks for your help.

My Solution is:

function translatePigLatin(str) {
let vowelChar = /[aeiou]/g;
var firstVowelIndex;
let newStr = “”;
console.log(firstVowelIndex);
if(!vowelChar.test(str)){
newStr = str + “ay”;
}
else{
firstVowelIndex = str.indexOf(str.match(vowelChar)[0]);
if(firstVowelIndex >0){
newStr = str.slice(firstVowelIndex) + str.slice(0,firstVowelIndex)+ “ay”;
}
if(firstVowelIndex == 0) {
newStr = str + “way”;
}
}
return newStr;
}
translatePigLatin(“consonant”);