PigLatin - Intermediate algorithm help

Hi, I have the following code so far and I believe my conditional statement section is not working properly. My approach is to use the regex pattern of /[aeiou]/i and find the index position for it in the string passed to the function. Then, I use that info along with substr() to put together the translated word.

Note: The sample tests provided which start with a vowel do not pass as well as the case where there are no vowels in the string.

Code so far:

function translatePigLatin(str) {
  var regex = /[aeiou]/i
  var found = str.match(regex)
  var translate = ''

  //Use to locate position of regex pattern
  //console.log(str.length) 
  //console.log(found)
  //console.log(found.index)

  if (str.substr(found.index) == 0) {
    translate = str + 'way';
    console.log(str + ' ' + translate)
  }
  if (str.substr(found.index) == -1) {
    translate = str + 'way';
    console.log(str + ' ' + translate)
  }
  else {
    translate = str.substr(found.index)+str.substr(0,found.index) + 'ay';
  console.log(str + ' ' + translate)
  }
  return translate
}

This is what the output is from the console.log() statements [string followed by space then translation]:

consonant onsonantcay
california aliforniacay
paragraphs aragraphspay
glove oveglay
algorithm algorithmay
eight eightay
schwartz artzschway

Sample tests and result

translatePigLatin("consonant");  //pass
translatePigLatin("california"); //pass
translatePigLatin("paragraphs"); //pass
translatePigLatin("glove"); //pass
translatePigLatin("algorithm"); //fail
translatePigLatin("eight"); //fail
translatePigLatin("schwartz"); //pass

For the case where there are no vowels (i.g. “rhythm”), the testing results in the following message:

TypeError: null is not an object (evaluating 'found.index')

I’ve got some pointers.

First, name your variables in a way that describes their purpose. If I see a variable named regex, I know it’s a regular expression, but what is it meant to find? Similarly, found contains what? If you wrote the code, maybe you know it’s a vowel, but you should be writing your code knowing that other people will read it, and you need to help them understand it with good names.

Second, it looks like you care about the index at which the first vowel in the string is found (if one is found), so I think the search method would be a better fit than match.

Third, remember that you can use return even when inside an if block.

1 Like

One more thing, substr is deprecated so you should use either slice or substring.

Thank you for the pointers, helped a lot and reduced the amount of code. I went with your suggestion of using the search method instead of index as well as renaming of the variables for clarity. They were a big help, all tests have now passed. :slightly_smiling_face:

Good job!

Sorry I kinda went on a rant about the naming thing, but it’s something that I think many beginners aren’t aware of.

const vowel = /[aeiou]/;
const firstVowelIndex = str.search(vowel);

if (firstVowelIndex...

Looking at this it’s clear that the first occurrence of a vowel is significant to the problem, even if you have no idea what /[aeiou]/ or str.search do, you can just assume they get the firstVowelIndex somehow and then follow the logic of the if statement(s).

Naming like this is important for your teammates and for your future self. If you join a team, there are instances where you write some code, and then a month later someone else has to make some changes. Or maybe one year later you have to make some changes yourself (but have completely forgotten about your thought process when you first wrote it). In both of these cases, good naming is extremely helpful.

1 Like