Pig Latin if/else Confusion

Hey all,

I’m having some confusion as to why my solution to the Pig Latin Intermediate Algorithm is not working. It tests positive for everything but glove (oveglay). Can anyone tell me 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");

I would recommend you add a console.log() just before you return. Take a look at your result: instead of oveglay, you’re returning lovegay. Same with schwartz and rhythm.

Thanks! I fixed that part. Now I’m struggling with getting the last checks for handling a word with a vowel at the end of it and handling words without vowels. Don’t my first and last statements handle that though?

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


  if(str[0].match(regex)){
    newStr = str.concat(strWay);
  } else if (str[0].match(regex) === null && str[1].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 letter2 = arr.splice(0,2);
    let str2 = letter2.join("");
    newStr = arr.join("").concat(str2).concat(strAy);
  }
  return newStr;
}

translatePigLatin("consonant");

So what do you think when you hear “Consonant cluster” in the instructions? You seem to be using the first two consonants. So glove becomes oveglay, as it ought. However Schwartz is becoming hwartzShay, when it should be artzSchway – the cluster is “Schw”.

Fixing that will also help with your “no vowels” problem: rhythm should be rhythmay, as it has NO vowels. You’re going with the first two consonants, giving you ythmrhay.

Suggestion? Use your regex to get the index of the first vowel – the consonant cluster is everything before that.

Again, just before you return, I might highly recommend you insert:

  console.log("str: ",str,", newStr: ",newStr);

this will show you a “before and after” snapshot of what your function took in, and is sending back. Handy to know.