Translate a string to Pig Latin

We are going to write a function called pigIt that will accept a string, str, as an argument.

You are given a string and the goal of the function is to translate the string to Pig Latin. To translate the string, you do the following:

  1. Move the first letter of the word to the end of the word.
  2. Add “ay” to the end of the word.

That’s it. If the word is only a single letter, skip step number one and just add an “ay” at the end. If the string is a punctuation mark or a number, leave it as is. Leave the cases of the words untouched.

Example:

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

To begin, we will split the string into an array where each word is its own array element. We assign that array to strArr.

let strArr = str.split(' ');

Next, we will create an empty array called pigLatin. This is the array we will append each word to after we translate it to Pig Latin.

let pigLatin = [];

#coding #programming #javascript #algorithms

JavaScript Algorithm: Simple Pig Latin
1.90 GEEK