You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+55Lines changed: 55 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -477,27 +477,82 @@ __code Implementation:__
477
477
478
478
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”).
479
479
480
+
```js
481
+
// Convert string to lowercase
482
+
str = str.toLowerCase()
483
+
```
484
+
480
485
Next, we initialize two variables:
481
486
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
+
482
494
vowels - containing the five English vowels
483
495
vowelIndex - for storing the index at which the first vowel in the word is found. It is initialized to 0.
496
+
484
497
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.
485
498
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
+
486
509
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.
487
510
511
+
```js
512
+
// If the first letter isn'tavoweli.eisaconsonant
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
+
488
523
At the last line, we use the .slice() method to manipulate the string to generate the Pig Latin equivalent.
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.
493
532
494
533
495
534
Our solution comprises mainly of two parts as analyzed below:
496
535
536
+
```js
537
+
str.replace(/^([aeiouy])(._)/, '$1$2way')
538
+
```
539
+
497
540
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.
498
541
542
+
```js
543
+
str.replace(/^(_[_^aeiouy]+)(._)/, '$2$1ay')
544
+
```
545
+
499
546
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.
500
547
548
+
```js
549
+
functionpigLatin_declarative(str) {
550
+
return str
551
+
.replace(/^([aeiouy])(._)/, '$1$2way')
552
+
.replace(/^(_[_^aeiouy]+)(._)/, '$2$1ay')
553
+
}
554
+
```
555
+
501
556
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.
0 commit comments