File tree Expand file tree Collapse file tree 3 files changed +46
-0
lines changed
src/fcc-intermediate-algorithms
tests/fcc-intermediate-algorithms Expand file tree Collapse file tree 3 files changed +46
-0
lines changed Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ From the Freecodecamp Javascript Certification Intermediate Algorithms module
9
9
- [ Seek and Destroy] ( #seek-and-destroy )
10
10
- [ Wherefore Art Thou] ( #wherefore-art-thou )
11
11
- [ Spinal Tap Case] ( #spinal-tap-case )
12
+ - [ Pig Latin] ( #pig-latin )
12
13
13
14
#### Sum All Numbers In a Range
14
15
@@ -80,3 +81,28 @@ export function spinalCase(str) {
80
81
return str .split (/ [_\W ] | (?=[A-Z ] )/ ).join (' -' ).toLowerCase ();
81
82
}
82
83
```
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
+ ```
Original file line number Diff line number Diff line change
1
+ export function translatePigLatin ( str ) {
2
+ if ( str === "" ) {
3
+ return "" ;
4
+ }
5
+
6
+ if ( / ^ [ a e i o u ] / . test ( str ) ) {
7
+ return str + 'way' ;
8
+ }
9
+
10
+ let splitStr = str . split ( / ( [ a e i o u ] .* ) / )
11
+ return [ splitStr [ 1 ] , splitStr [ 0 ] , 'ay' ] . join ( '' ) ;
12
+ }
Original file line number Diff line number Diff line change
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments