Skip to content

Commit e48fe20

Browse files
committed
Complete pig latin algorithm
1 parent 251b2db commit e48fe20

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

src/fcc-intermediate-algorithms/fcc-intermediate-algorithms.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ From the Freecodecamp Javascript Certification Intermediate Algorithms module
99
- [Seek and Destroy](#seek-and-destroy)
1010
- [Wherefore Art Thou](#wherefore-art-thou)
1111
- [Spinal Tap Case](#spinal-tap-case)
12+
- [Pig Latin](#pig-latin)
1213

1314
#### Sum All Numbers In a Range
1415

@@ -80,3 +81,28 @@ export function spinalCase(str) {
8081
return str.split(/[_\W]|(?=[A-Z])/).join('-').toLowerCase();
8182
}
8283
```
84+
85+
#### Pig Latin
86+
87+
Translate the provided string to pig latin.
88+
89+
Pig Latin takes the first consonant (or consonant cluster) of an English word, moves it to the end of the word and suffixes an "ay".
90+
91+
If a word begins with a vowel you just add "way" to the end.
92+
93+
Input strings are guaranteed to be English words in all lowercase.
94+
95+
```javascript
96+
export function translatePigLatin(str) {
97+
if (str === "") {
98+
return "";
99+
}
100+
101+
if (/^[aeiou]/.test(str)) {
102+
return str + 'way';
103+
}
104+
105+
let splitStr = str.split(/([aeiou].*)/)
106+
return [splitStr[1], splitStr[0], 'ay'].join('');
107+
}
108+
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export function translatePigLatin(str) {
2+
if (str === "") {
3+
return "";
4+
}
5+
6+
if (/^[aeiou]/.test(str)) {
7+
return str + 'way';
8+
}
9+
10+
let splitStr = str.split(/([aeiou].*)/)
11+
return [splitStr[1], splitStr[0], 'ay'].join('');
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { translatePigLatin } from "../../src/fcc-intermediate-algorithms/pig_latin";
2+
3+
test('should translate pig latin', () => {
4+
expect(translatePigLatin("")).toBe("");
5+
expect(translatePigLatin("onset")).toBe("onsetway");
6+
expect(translatePigLatin("tt")).toBe("ttay");
7+
expect(translatePigLatin("california")).toBe( "aliforniacay");
8+
});

0 commit comments

Comments
 (0)