DEV Community

Discussion on: JavaScript Challenge 1: Simple Pig Latin

Collapse
 
armanozak profile image
L. Arman Özak

Or this:

const pigIt = str => str.replace(/(\w)(\w+)/g, '$2$1ay');
Enter fullscreen mode Exit fullscreen mode
Collapse
 
metruzanca profile image
Samuele Zanca

You can do a very similar thing with array methods and rest operator:

function pig(sentence){
  return sentence
          .split(' ')
          .map(([f, ...rest]) => rest.join('') + f + 'ay')
          .join(' ')
}

Collapse
 
albertomontalesi profile image
AlbertoM

Yeah with RegEx you can do it very easily but I feel like it makes the code less readable.