added extra solution to pig latin guide (#34465)
* added extra solution to pig latin guide * Fixed repl.it link for Advanced solution * Remove repl links * Fix formatting issue
This commit is contained in:
committed by
Randell Dawson
parent
25ea44edf5
commit
a412195e38
@ -66,7 +66,6 @@ You will need to use everything you know about string manipulation to get the la
|
||||
// test here
|
||||
translatePigLatin("consonant");
|
||||
|
||||
 <a href='https://repl.it/CLmt/0' target='_blank' rel='nofollow'>Run Code</a>
|
||||
|
||||
### Code Explanation:
|
||||
|
||||
@ -130,7 +129,6 @@ You will need to use everything you know about string manipulation to get the la
|
||||
// test here
|
||||
translatePigLatin("consonant");
|
||||
|
||||
 <a href='https://repl.it/CLmw/0' target='_blank' rel='nofollow'>Run Code</a>
|
||||
|
||||
### Code Explanation:
|
||||
|
||||
@ -148,56 +146,28 @@ You will need to use everything you know about string manipulation to get the la
|
||||
##  Advanced Code Solution:
|
||||
|
||||
function translatePigLatin(str) {
|
||||
var strArr = [];
|
||||
var tmpChar;
|
||||
|
||||
// check if the char is consonant using RegEx
|
||||
function isConsonant(char) {
|
||||
return !/[aeiou]/.test(char);
|
||||
}
|
||||
|
||||
// return initial str + "way" if it starts with vowel
|
||||
// if not - convert str to array
|
||||
if (!isConsonant(str.charAt(0)))
|
||||
return str + "way";
|
||||
else
|
||||
strArr = str.split("");
|
||||
|
||||
// push all consonats to the end of the array
|
||||
while (isConsonant(strArr[0])) {
|
||||
tmpChar = strArr.shift();
|
||||
strArr.push(tmpChar);
|
||||
}
|
||||
// convert array to string and concatenate "ay" at the end
|
||||
return strArr.join("")+"ay";
|
||||
return str.replace(/^[aeiou]\w*/, "$&way").replace(/(^[^aeiou]+)(\w*)/, "$2$1ay");
|
||||
}
|
||||
|
||||
// test here
|
||||
translatePigLatin("consonant");
|
||||
|
||||
 <a href='https://repl.it/CLmv/0' target='_blank' rel='nofollow'>Run Code</a>
|
||||
|
||||
### Code Explanation:
|
||||
|
||||
* `isConsonant()` is used to check if a character is a consonant.
|
||||
* If first character is vowel, add **way** to end of string and return it.
|
||||
* If first character is not a vowel:
|
||||
* Split string into array using `split()`.
|
||||
* Push all consonants to end of array with help of `shift()` and `push()`.
|
||||
* Convert array to string using `join()` and add **ay** to end of string. Return it.
|
||||
* Use `replace()` on the string, using a regular expression to check if the first letter is a consonant and adding **way** at the end in this case. If the first letter is a consonant nothing will happen at this point.
|
||||
* Use `replace()` again to check for consonants at the beginning of the word and to move it or them to the end of the word and add **ay** at the end.
|
||||
|
||||
#### Relevant Links
|
||||
|
||||
* <a href='http://forum.freecodecamp.com/t/javascript-string-prototype-split/15944' target='_blank' rel='nofollow'>JS String Prototype Split</a>
|
||||
* <a href='http://forum.freecodecamp.com/t/javascript-array-prototype-shift/14301' target='_blank' rel='nofollow'>JS Array Prototype Shift</a>
|
||||
* <a href='http://forum.freecodecamp.com/t/javascript-array-prototype-push/14298' target='_blank' rel='nofollow'>JS Array Prototype Push</a>
|
||||
* <a href='http://forum.freecodecamp.com/t/javascript-array-prototype-join/14292' target='_blank' rel='nofollow'>JS Array Prototype Join</a>
|
||||
* <a href='https://guide.freecodecamp.org/javascript/regular-expressions-reference'>Regular Expressions Reference</a>
|
||||
* <a href='https://guide.freecodecamp.org/miscellaneous/regular-expressions-resources/'>Regular Expressions Resources</a>
|
||||
|
||||
###  Credits:
|
||||
|
||||
If you found this page useful, you may say thanks to the contributors by copying and pasting the following line in the main chat:
|
||||
|
||||
**`Thanks @Rafase282 @sabahang @aganita @Hallaathrad @finally_static for your help with Algorithm: Pig Latin`**
|
||||
**`Thanks @Rafase282 @sabahang @aganita @Hallaathrad @finally_static @rulamon for your help with Algorithm: Pig Latin`**
|
||||
|
||||
##  NOTES FOR CONTRIBUTIONS:
|
||||
|
||||
|
Reference in New Issue
Block a user