Pig Latin - Solved, but parts won't chain

Tell us what’s happening:
Hello! I put together the following code for the Pig Latin challenge. It works, but there are some parts that I don’t understand.

  • I thought I should be able to chain everything, but I can’t get it to work if it’s not broken down how I have it now.

  • I tried to compare this to the Get a Hint Solutions but my approach is very different. Don’t be shy to let me know if/why my differences are bad practice :slight_smile:

Your code so far


function translatePigLatin(str) {
  //Split  str at the first vowel.
  let consonant = str.split(/([aeiou].*)/i); // → [gl,ove,]
  
  //First group of consonants shifts to the end
  consonant.push(consonant.shift()); //Why can't this be chained above?

  /* Doesn't work
 consonant.join(); // → ove,,gl instead of ovegl like in the template literal below
 */

  return str.match(/\b[aeiou]\w+/)
    ? `${str}way`
    : `${consonant.join("")}ay`; // consonant.join("") → ovegl
}

Code without comments


function translatePigLatin(str) {
  let consonant = str.split(/([aeiou].*)/i); 
  consonant.push(consonant.shift()); 

  return str.match(/\b[aeiou]\w+/)
    ? `${str}way`
    : `${consonant.join("")}ay`;
}

Your browser information:

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

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

Remember that push() returns the length of the array, not the new array. That’s why you needed create consonant as an array before you mutate it with push.

1 Like

Array.prototype.join returns the ‘chained’ string, it doesnt mutate the array.

let myArr = ['a','b','c'];

console.log(myArr.join()); /*logs 'abc'*/
console.log(myArr); /*still logs ['a','b','c']*/

myArr = myArr.join(); /*This should fix the join problem I think*/

console.log(myArr); /*logs 'abc'*/

Thank you! I wasn’t thinking about what .push returned being the length.

consonant = consonant.join(''); will work as expected and is what happens inside the template literal as well. Without any parameter for join, it will also join the comma’s separating the entries in the array.

edit:
join returns a new string, so it has to be assigned.

This is quite a fun challenge to revisit and retry.
I’ve added my latest solution, I give it a 5 for readabilty :wink:, could do with some comments as well.

function translatePigLatin(str) {
  const suffix = str.search(/([aeiou].*)/i) ? 'ay' : 'way';
  return /([aeiou].*)/i.test(str) ? str.slice(str.search(/([aeiou].*)/i)) + str.slice(0, str.search(/([aeiou].*)/i)) + suffix : str + suffix;
}

@steven_kuipers @M-Michelini THANK YOU!! You both nailed it. I wasn’t considering what was being returned. (plus I forgot about the built-in reverse) Check out where I got with your help! I think it’s pretty good.

const translatePigLatin = str => {
  let shiftConsonants = str.split(/([aeiou].*)/).reverse().join(''); 

  return str.match(/\b[aeiou]\w+/)
    ? `${str}way`
    : `${shiftConsonants}ay`; 
}
1 Like

I like how you worked out the suffixes. I’m going to have to remember that move for the future. Instead of having it hard coded like I do.

@steven_kuipers In the future, please do not post solutions. Instead, just stick with hints and examples to guide the OP.

Thank you.

Lol, I thought I was wrong so deleted my comment. All good.