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
+105-4Lines changed: 105 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -447,15 +447,116 @@ method tests whether all elements pass the test or not which is implemented by p
447
447
<hr>
448
448
<hr>
449
449
450
-
<b>6. Name </b>
450
+
<b>6. Pig Latin Translator</b>
451
451
452
-
__The challenge:__ <p> </p>
452
+
For specific information on Pig Latin, view this [article](https://en.wikipedia.org/wiki/Pig_Latin).
453
453
454
+
__The challenge:__ <p>Convert a string of text into Pig Latin.</p>
454
455
455
-
__Algorithmic Thinking:__ <p> </p>
456
456
457
+
__Algorithmic Thinking:__
458
+
459
+
We will consider two(2) ways to implement this function in JavaScript. They are:
460
+
461
+
An imperative approach
462
+
A declarative approach
463
+
Before going ahead to implement both solutions, it’d be helpful to gain a better understanding of the two terms used above.
464
+
465
+
Imperative vs Declarative
466
+
Very often, we find these terms thrown around like they are very simple concepts everyone should know. However, the difference is usually not much obvious to most.
467
+
468
+
Simply put, an imperative style of programming is one which specifies how things get done. Although this might sound like what you do each time you write code, there'sadifferencetoit. Imagineyouweretoaddanarrayofnumbersandreturnthesum, therearedifferentwaysyoucouldapproachtheproblem. Onewaycouldbewritingaforloopthat'd go over each element in the array and cumulatively add every element to an accumulator until the final sum is reached. That is imperative. You are specifying how things get done.
469
+
470
+
On the other hand, a declarative approach would abstract this process, allowing you to specify what should be done rather than how. Thus, you may use the .reduce() method on the array to reduce every element to a final value by returning the sum within the call back.
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
+
480
+
```js
481
+
// Convert string to lowercase
482
+
str = str.toLowerCase()
483
+
```
484
+
485
+
Next, we initialize two variables:
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
+
494
+
vowels - containing the five English vowels
495
+
vowelIndex - for storing the index at which the first vowel in the word is found. It is initialized to 0.
496
+
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.
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
+
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.
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
+
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.
532
+
533
+
534
+
Our solution comprises mainly of two parts as analyzed below:
535
+
536
+
```js
537
+
str.replace(/^([aeiouy])(._)/, '$1$2way')
538
+
```
539
+
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.
541
+
542
+
```js
543
+
str.replace(/^(_[_^aeiouy]+)(._)/, '$2$1ay')
544
+
```
545
+
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.
547
+
548
+
```js
549
+
functionpigLatin_declarative(str) {
550
+
return str
551
+
.replace(/^([aeiouy])(._)/, '$1$2way')
552
+
.replace(/^(_[_^aeiouy]+)(._)/, '$2$1ay')
553
+
}
554
+
```
555
+
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