DEV Community

Cover image for JavaScript Challenge 1: Simple Pig Latin
AlbertoM
AlbertoM

Posted on • Edited on • Originally published at inspiredwebdev.com

JavaScript Challenge 1: Simple Pig Latin

This article was originally posted on my blog. Head over to inspiredwebdev.com for more articles and tutorials. Check out my JavaScript course on Educative to learn everything from ES6 to ES2020.

 

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

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(" ");
Enter fullscreen mode Exit fullscreen mode

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`
})
Enter fullscreen mode Exit fullscreen mode

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:

word.match(/[A-z]/i)

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(' ');
}
Enter fullscreen mode Exit fullscreen mode

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.


If you want to learn everything about JavaScript from ES6 all the way to ES2020, please check out my book available to read for free on Github. A course is also on Educative

Alt Text

 

Top comments (14)

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.

Collapse
 
pentacular profile image
pentacular

You might find it's simpler if you rearrange it slightly. :)

const latinifyWord = (word) =>
  word.match(/[A-z]/i)
    ? `${word.substr(1)}${word.substr(0,1)}ay`
    : word;

const pigIt = (str) =>
  str.split(' ').map(latinifyWord).join(' ');
Collapse
 
albertomontalesi profile image
AlbertoM

Yep, that also works :)

Collapse
 
wildh0g profile image
WildH0g • Edited

Is it just me or does your RegEx not work? I mean what you have is this:

/[A-z]/i

And it's basically saying does my word have at least one letter in it? So it if you have a semicolon attached to the end of your word, it will not be ignored. I think what you meant was someting like this:

/^[A-z]+$/i

That is if you wanted to ignore words with characters altogether.

Although what I thought when I read the task was that you were meant to rearrange the letters and keep the punctuation marks at the end, if any.

Collapse
 
albertomontalesi profile image
AlbertoM

Uhm, I think what the task meant with leave the punctuation marks untouched was in the case of them being alone, so if you have ! you don't want to end up with !ay. My solution would transform hello! to ello!hay which may be wrong, you are right. They should have had stricter tests and a better description on CodeWars lol, they have no test case for a punctuation mark attached to a string.

Collapse
 
buggy1887 profile image
buggy1887

Great explanation, thanks.

Collapse
 
albertomontalesi profile image
AlbertoM

Thanks, i'm happy you liked it

Collapse
 
jagzviruz profile image
Jagadish K.

Please note .. this is not the complete rules for PigLatin ..

  • For words that begin with consonant sounds, the consonant before the initial vowel should be moved to the end of the word sequence and "ay" affixed. E.g "pig" = "igpay"
  • For words that begin with consonant clusters, the clusters should be moved to the end of the word sequence and "ay" affixed. E.g "glove" = "oveglay"
  • For words that begin with vowel sounds, simply add "way" to the end of the word. E.g "explain" = "explainway”

Have a look at scotch.io/courses/the-ultimate-gui...

Collapse
 
albertomontalesi profile image
AlbertoM

Yes, i've taken it from CodeWars, that's why it's called 'Simple Pig Latin', it's a simpler version.

Collapse
 
raounek profile image
touibeg mohamed

thank you i liked...

Collapse
 
albertpak profile image
Al

This is awesome! Keep 'em coming!

Collapse
 
albertomontalesi profile image
AlbertoM

Thanks!, I will