Skip to content

Latest commit

 

History

History
40 lines (32 loc) · 847 Bytes

simple-pig-latin.md

File metadata and controls

40 lines (32 loc) · 847 Bytes

Description

Move the first letter of each word to the end of it, then add "ay" to the end of the word. Leave punctuation marks untouched.

Examples

pigIt('Pig latin is cool'); // igPay atinlay siay oolcay
pigIt('Hello world !');     // elloHay orldWay !

Kata's link: Simple Pig Latin

Best Practices

First:

function pigIt(str){
  return str.replace(/(\w)(\w*)(\s|$)/g, "\$2\$1ay\$3")
}

Second:

pigIt = s => s.split(' ').map(e => e.substr(1) + e[0] + 'ay').join(' ');

My solutions

function pigIt(str){
  //Code here
  str = str.trim().split(/\s{1,}/);
    return str.map(val => {
        if (/^[A-Za-z]+$/.test(val)) {
            return `${val.slice(1)}${val.slice(0, 1)}ay`;
        }
        return val;
    }).join(' ');
}