Simple Pig Latin

Approach

  1. split all words

  2. iterates process-1

    1. if words length equal to 1 and is not alphabet return words without processing

    2. return (From the second letter of the word to the end) + (first letter of letter) + 'ay'

  3. return process-2 join with space

Solution

const pigIt = (str) => str.split(' ')
  .map((v) => {
    if (v.length === 1 && !v.match(/[a-z]/i)) {
      return v;
    }
    
    return `${v.slice(1)}${v[0]}ay`;
  })
  .join(' ');

Last updated

Was this helpful?