-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsimple_pig_latin.js
More file actions
39 lines (36 loc) · 2 KB
/
simple_pig_latin.js
File metadata and controls
39 lines (36 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/******************************************************************************************
* CODEWARS SIMPLE PIG LATIN CHALLENGE *
* *
* Problem Statement *
* 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 *
* Input 1: "Pig latin is cool" *
* Output 1: igPay atinlay siay oolcay *
* *
* Input 2: "Hello world !" *
* Output 2: elloHay orldway ! *
* *
*****************************************************************************************/
function pigIt(str) {
let simplePigLatin = new String("");
let splitString = str.trim().split(" ");
let puntuationFound = false;
for (let i = 0; i < splitString.length; i++) {
puntuationFound = false;
for (let j = 0; j < splitString[i].length; j++) {
if (
splitString[i][j].toLowerCase() >= "a" &&
splitString[i][j].toLowerCase() <= "z"
) {
if (j > 0) simplePigLatin += splitString[i][j];
} else {
puntuationFound = true;
}
}
if (puntuationFound === false) simplePigLatin += splitString[i][0] + "ay ";
else simplePigLatin += splitString[i][0];
}
return simplePigLatin.trim();
}