Intermediate Algorithm Scripting - Pig Latin

Tell us what’s happening:
Another regex, another hard watch!! Solved this myself, but I think the code is too long. Could have shortened it, but I feel like we can go with this. What you think?

Your code so far

function translatePigLatin(str) {
  let vowel = /[aoiue]+/
  let c = /[^aoiue]+/
let x = [...str.split('')]
let o = str.match(c)

  if (str.match(vowel) !== null && str.match(vowel).index !== 0) {
   let z = (x.slice(0, o[0].length)).join('')
    return  x.slice(str.match(c).index+ z.length ).join('') + z + 'ay' 
  }
  else if (str.match(vowel)== null) {
    return str + 'ay'
  }
else {
  return str + 'way'
}
 

}

console.log(translatePigLatin("rhythm"));

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15

Challenge: Intermediate Algorithm Scripting - Pig Latin

Link to the challenge:

Please use spoiler tags when posting solutions.


I would stop and think about how easy your code is to understand.

Here is your exact same logic with some minor tweaks:

function translatePigLatin(str) {
  const vowels         = /[aoiue]+/;
  const consonants     = /[^aoiue]+/;
  const letters        = [...str.split('')];
  const firstVowel     = str.match(vowels);
  const firstConsonant = str.match(consonants);

  if (!firstVowel) {
    // No vowels in the word
    return str + 'ay';
  } else if (firstVowel.index === 0) {
    // First letter is a vowel
    return str + 'way';
  } else {
    // First letter is a consonant
    const beforeFirstVowel = letters
      .slice(0, firstConsonant[0].length)
      .join('');
    const firstVowelAndAfter = letters.slice(firstVowel.index).join('');
    return firstVowelAndAfter + beforeFirstVowel + 'ay';
  }
}

I made these changes:

  1. Use standard formatting

  2. Use human readable variable names

  3. Use variables (with those readable names) in place of long expressions

  4. Add a couple of small comment explaining the overall logical flow

  5. Reorder if cases to simplify conditionals

  6. Using const instead of let everywhere possible (this indicates the data in the variable won’t change)

I’m sure it could be tidied more, but now other people can read and understand the logic much more easily. These sorts of changes are important professionally because you won’t remember everything in your head right now about this problem a couple of months from now. The less clear your variable names and code structure, the harder it is to change in the future.

1 Like

In old legacy way:

function translatePigLatin(str) {
  let vowelArr=['a','e','i','o','u'], consonants=/[^aeiou]/i;
  //checking whether first char is vowel
  if(vowelArr.includes(str[0])){
    return str+'way'
  }
  //checking if first char is consonant
  else if(consonants.test(str[0])){
      for(let i=0;i<str.length;i++){
     if(vowelArr.includes(str[i])){ 
      return str.slice(i)+str.slice(0,i)+'ay'
  }
  }
  return str+'ay'
  }
  //output any other first characters
 return str;
}

We can still shorten the code with many approaches but the above code is , for those who like to use naive approach.
Happy Coding !!

1 Like

As mentioned above, please use spoiler tags.

Also please include discussion relevant to OP’s code. The purpose of code feedback threads is to provide feedback, not to show your alternate code.


As mentioned above, I’d also recommend you use standard formatting for your code.

function translatePigLatin(str) {
  const vowels = ['a', 'e', 'i', 'o', 'u'];

  if (vowels.includes(str[0])) {
    // First char is vowel
    return str + 'way';
  } else {
    // First char is consonant
    for (let i = 0; i < str.length; i++) {
      if (vowels.includes(str[i])) {
        return str.slice(i) + str.slice(0, i) + 'ay';
      }
    }
    // No vowels in str
    return str + 'ay';
  }
}

You can make similar improvements to your code as I noted above.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.