Skip to content

Commit ef4f0a5

Browse files
authored
Add code snippets of Pig Latin
1 parent f59b53d commit ef4f0a5

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,27 +477,82 @@ __code Implementation:__
477477
478478
We start by converting the received string str to lowercase. This is to prevent any casing related errors during comparison(“a” does not equal “A”).
479479
480+
```js
481+
// Convert string to lowercase
482+
str = str.toLowerCase()
483+
```
484+
480485
Next, we initialize two variables:
481486
487+
```js
488+
// Initialize array of vowels
489+
const vowels = ["a", "e", "i", "o", "u"];
490+
// Initialize vowel index to 0
491+
let vowelIndex = 0;
492+
```
493+
482494
vowels - containing the five English vowels
483495
vowelIndex - for storing the index at which the first vowel in the word is found. It is initialized to 0.
496+
484497
We use an if…else statement to check if the first letter of the word can be found within our vowels array by calling the .includes() method on the array while passing it the first letter of the string str[0]. If it is found, this returns true, which implies that the first letter is a vowel. Hence, we simply add "``way``" to the end of the string and return the result as the Pig Latin equivalent.
485498
499+
500+
```js
501+
if (vowels.includes(str[0])) {
502+
// If first letter is a vowel
503+
return str + "way";
504+
} else {
505+
...
506+
}
507+
```
508+
486509
If the statement evaluates to false, it signifies that the starting character is a consonant. Hence, we use a for…of loop to iterate through the string to identify the position of the first vowel. When we locate the first vowel, we use the .indexOf() method to retrieve it’s position in the string and store it into the variable vowelIndex. After this step we terminate the loop using the break statement.
487510
511+
```js
512+
// If the first letter isn't a vowel i.e is a consonant
513+
for (let char of str) {
514+
// Loop through until the first vowel is found
515+
if (vowels.includes(char)) {
516+
// Store the index at which the first vowel exists
517+
vowelIndex = str.indexOf(char);
518+
break;
519+
}
520+
}
521+
```
522+
488523
At the last line, we use the .slice() method to manipulate the string to generate the Pig Latin equivalent.
489524
525+
```js
526+
return str.slice(vowelIndex) + str.slice(0, vowelIndex) + "ay";
527+
```
528+
490529
2. Declarative Approach
491530
492531
In this approach, we implement a very concise solution to this challenge by combining the .replace() method and regular expressions to transform the received string into its Pig Latin equivalent.
493532
494533
495534
Our solution comprises mainly of two parts as analyzed below:
496535
536+
```js
537+
str.replace(/^([aeiouy])(._)/, '$1$2way')
538+
```
539+
497540
The first .replace statement specifies a replacement to be carried out if the word begins with a vowel. This is specified in the first bracket within the_ *.replace()* method call i.e *([aeiou])*. The second bracket *(.*)* refers to every other character after the vowel. Thus, the expression specifies a pattern for words beginning with a vowel and followed by anything else. When this case is matched, a new string in the format of '``$1$2way``' is generated and used to replace the original srtring. $1 here refers to the first bracket and $2, the second bracket. This means that we simply take the word as it was and affix "``way``" to the end.
498541
542+
```js
543+
str.replace(/^(_[_^aeiouy]+)(._)/, '$2$1ay')
544+
```
545+
499546
The second .replace statement specifies that if the word does not start with a vowel i.e ^([aeiouy]+), and is followed by anything else (``*.*)*, it should be replaced with a string formatted in the order '$2$1ay``'. The plus sign in ^([aeiouy]+) caters for a situation where there is a consonant cluster. Thus it represents every non-vowel character at the start of the word. '$2$1ay' generates the new string in the order of remaining characters + consonant cluster + '``ay``'. This gives the Pig Latin equivalent.
500547
548+
```js
549+
function pigLatin_declarative(str) {
550+
return str
551+
.replace(/^([aeiouy])(._)/, '$1$2way')
552+
.replace(/^(_[_^aeiouy]+)(._)/, '$2$1ay')
553+
}
554+
```
555+
501556
Note that we chain both .replace() methods in succession such that both cases are tested and only the one that matches will be evaluated further.
502557
503558
[Source](https://scotch.io/courses/the-ultimate-guide-to-javascript-algorithms/pig-latin)

0 commit comments

Comments
 (0)