HomeTutorialsArticlesAbout MeContact

JS Challenge 1: Simple Pig Latin

By Alberto Montalesi
Published in Challenge
September 29, 2020
1 min read
Check out my Github for my free-to-read JavaScript Ebook that covers all the new features from ES6 to 2021. If you want find a great place for interactive tutorials, i recommend Educative where you can find my JavaScript course
This website contains affiliate links.

  In this article we will solve together the Simple Pig Latin challenge from CodeWars, you can find it at this link. the difficulty of this challenge is medium.

Let’s read the task together:

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

The first example given to us is this one

pigIt('Pig latin is cool'); // igPay atinlay siay oolcay

Alright, after reading it one time we can already see different ways of solving this problem:

  • Using RegEx to remove the first character after space and to add ay at the end of each word
  • Splitting the string in an array and iterating over each portion

RegEx can be very powerful and allow you to do a lot in very little but they can also get very hard to read for your others and also for yourself going back to a project after several times.

Let’s go with the more basic approach of using JavaScript to iterate over the string and perform our modifications.

First, let’s split our string into an array:

const arr = str.split(" ");

Next, we want to iterate over the array, removing the first character and appending it at the end, followed by ay.

arr.map((word) => {
  return `${word.substr(1)}${word.substr(0, 1)}ay`

Here we are iterating over the strings with map and at each iteration, we remove the first character with substr(1) which will return us a substring from character 1 to the end of the string, then we add the first letter and finally we append ay at the end of it.


The only problem with this implementation is that it will not skip characters such as !,? etc… Let’s use a very simple RegEx to determine if our character is a letter.

We can implement a simple check like the following:


This will ensure that only characters from a-z will be taken into account

Now let’s put everything together:

function pigIt(str) {
  const arr = str.split(' ')
  return arr
    .map((word) => {
      return word.match(/[A-z]/i)
        ? `${word.substr(1)}${word.substr(0, 1)}ay`
        : word
    .join(' ')

There you have it, a simple functions that will:

  • iterate over each word in a string
  • remove the first letter of said string and add it at the end of it
  • append ay at the end of each word

If you liked this type of content, please let me know in the comments and I’ll create more of these.

Thank you very much for reading, if you enjoyed this article, please share it with friends and colleagues and if there is a topic you would like me to cover, reach out to me on twitter at @montalesi. Follow me on DevTo or on Twitter for more.

complete guide to modern javascript alberto montalesi ebook bannerGet my ebook on Leanpub or get my course on Educative


Previous Article
Everything new coming in ES2022

Alberto Montalesi

Software Developer

buy me coffee
complete guide to modern javascript book cover

Complete Guide to Modern JavaScript

Get the Course



Related Posts

JS Challenge 7: Multiples of 3 or 5
October 28, 2020
1 min
© 2022, All Rights Reserved.

Quick Links


Social Media