10000 Finish pigLatin · JonathanYiv/javascript-exercises@2201218 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2201218

Browse files
committed
Finish pigLatin
1 parent 425f336 commit 2201218

File tree

3 files changed

+32
-21
lines changed

3 files changed

+32
-21
lines changed

pig_latin/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ turns them into pig latin. Please see the following wikipedia page for details r
33

44
https://en.wikipedia.org/wiki/Pig_Latin
55

6-
The rules section will give the rules and the examples that are required to complete this exercise.
6+
The rules section will give the rules and the examples that are required to complete this exercise.

pig_latin/pigLatin.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,37 @@
1-
function translate() {
2-
// body...
1+
function translate(text) {
2+
return text.split(" ").map(word => {
3+
const letters = word.split("");
4+
if (isVowel(letters[0])) {
5+
letters.push("a", "y");
6+
} else {
7+
while(isConsonant(letters[0]) || letters[0] === "u") {
8+
letters.push(letters.shift());
9+
}
10+
letters.push("a", "y");
11+
}
12+
return letters.join("");
13+
}).join(" ");
14+
}
15+
16+
function isVowel(character) {
17+
return !!character.match(/[aeiouAEIOU]/);
18+
}
19+
20+
function isConsonant(character) {
21+
return !isVowel(character);
322
}
423

524

625
module.exports = {
726
translate
827
}
928

29+
// Rule 1: If a word begins with a vowel sound, add an "ay" sound to the end of the word.
30+
31+
// Rule 2: If a word begins with a consonant sound, move it to the end of the word,
32+
// and then add an "ay" sound to the end of the word.
33+
34+
// (There are a few more rules for edge cases, and there are regional variants too,
35+
// but that should be enough to understand the tests.)
36+
37+
// See https://en.wikipedia.org/wiki/Pig_Latin for more details.

pig_latin/pigLatin.spec.js

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,3 @@
1-
// Topics
2-
3-
// * modules
4-
// * strings
5-
6-
// Pig Latin
7-
8-
// Pig Latin is a made-up children's language that's intended to be confusing. It obeys a few simple rules (below) but when it's spoken quickly it's really difficult for non-children (and non-native speakers) to understand.
9-
10-
// Rule 1: If a word begins with a vowel sound, add an "ay" sound to the end of the word.
11-
12-
// Rule 2: If a word begins with a consonant sound, move it to the end of the word, and then add an "ay" sound to the end of the word.
13-
14-
// (There are a few more rules for edge cases, and there are regional variants too, but that should be enough to understand the tests.)
15-
16-
// See https://en.wikipedia.org/wiki/Pig_Latin for more details.
17-
181
var pigLatin = require("./pigLatin.js");
192

203
describe('#translate', function() {
@@ -61,4 +44,4 @@ describe('#translate', function() {
6144
s = pigLatin.translate("the quick brown fox");
6245
expect(s).toEqual("ethay ickquay ownbray oxfay");
6346
});
64-
});
47+
});

0 commit comments

Comments
 (0)
0