Pig Latin_ error: Should handle words without vowels. But checked on ide it works

Tell us what’s happening:
I am getting error as title says: Should handle words without vowels.
I have checked on different enviroment it gives correct output without vowels.
I am not sure where it is the mistake. Also if you can tell me how to improve my code I’d appreciate it .
thanks in advance
Your code so far


function translatePigLatin(str) {
  let index= 0;
  if (str[0]==="a"||str[0]==="i"||str[0]==="o"||str[0]==="y"||str[0]==="e"){
    return str+"way";
    }
  else {
  for(let i of str){
    if(i === "a" || i === "i" || i === "o" || i === "y" || i === "e"){
      break;
      }
      index++;
    }
  }
  return str.slice(index)+str.slice(0,index)+"ay"
  
}

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) 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

does your code take a word like ‘my’ and give ‘myay’ ? That’s basically the requirement.

thanks for replying … Maybe I am not understanding the requirement. But,
should not be a “my” => “ymay”?
since on test “california” should return “aliforniacay” ??

yeah, i understand the confusion but if you re-read the instructions, specifically this line:

Pig Latin takes the first consonant (or consonant cluster) of an English word, moves it to the end of the word and suffixes an “ay”.

you see they refer to “consonant cluster” Therefore “my” is a consonant cluster and needs to ‘move’ back
and therefore essentially stays the same.

There are other examples of how to handle consonant clusters here:

1 Like

I think my code should work, but the last bit (the else statement) is not being reached. Can anyone explain why?

function translatePigLatin(str) {
  let newStr = "";
  let regex = /[aeiou]/gi;
  let strWay = "way";
  let strAy = "ay";

  if(str[0].match(regex)) {
    newStr = str.concat(strWay);
  } else if (str[0].match(regex) === null){
    let arr = str.split("");
    let letter = arr.splice(0,1);
    newStr = arr.join("").concat(letter).concat(strAy);
  } else {
    let arr = str.split("");
    let letter = arr.splice(0,2);
    newStr = arr.join("").concat(letter).concat(strAy);
  }
  return newStr;
}

translatePigLatin("consonant");

The match function returns either an array of matches or null. An if condition checks for Boolean results. So if you use the match function in an if condition, you’ll get true if you return something other than null. For clarity here, you may want to use the Regex.test() function instead since that returns a Boolean.

As written, your code will check the first letter of the provided string and see if it matches your regular expression. If it matches, then it will enter the if block. If not, it will enter the else-if block, but since those are really the only two options (a match or no match), there aren’t any cases (that I can think of) where the else block would be entered.

So for example, if str = ‘consonant’, we check the first if condition. str[0] does not match a, e, i, o, or u so we skip to the next if condition. str[0].match(/[aeiou]/gi) does equal null, so we enter the else-if block.

If str = ‘thrice’, str[0] does not match a, e, i, o, or u, so we skip to the next if condition. str[0].match(/[aeiou]/gi) does equal null, so here’s what happens next:

  • we convert ‘thrice’ to an array (note that strings already are arrays of consonants).
  • we remove the first letter of the [‘t’, ‘h’, ‘r’, ‘i’, ‘c’, ‘e’] and store it, so the variable letter now equals [‘t’] and arr now equals [‘h’, ‘r’, ‘i’, ‘c’, ‘e’].
  • we join the elements of arr (‘hrice’), then concatenate [‘t’] to the previous (‘hricet’), then concatenate ‘ay’ to the previous (‘hricetay’, which is not exactly what we want to return).

I spend a lot of time on MDN (Mozilla Developer Network) looking up JavaScript functions and suggest you take a second look at match, split, splice, join, and concat. Also take a look at the String.protoype methods search, slice, and substring as well as the Regex.prototype.test() method.

Otherwise, your code is looking great, @Neralizer. Keep up the good work!