diff --git a/guide/english/certifications/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/pig-latin/index.md b/guide/english/certifications/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/pig-latin/index.md index 50fe4d14f6..a13459a1e5 100644 --- a/guide/english/certifications/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/pig-latin/index.md +++ b/guide/english/certifications/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/pig-latin/index.md @@ -66,7 +66,6 @@ You will need to use everything you know about string manipulation to get the la // test here translatePigLatin("consonant"); -![:rocket:](https://forum.freecodecamp.com/images/emoji/emoji_one/rocket.png?v=3 ":rocket:") Run Code ### 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"); -![:rocket:](https://forum.freecodecamp.com/images/emoji/emoji_one/rocket.png?v=3 ":rocket:") Run Code ### Code Explanation: @@ -148,56 +146,28 @@ You will need to use everything you know about string manipulation to get the la ## ![:rotating_light:](https://forum.freecodecamp.com/images/emoji/emoji_one/rotating_light.png?v=3 ":rotating_light:") 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"); -![:rocket:](https://forum.freecodecamp.com/images/emoji/emoji_one/rocket.png?v=3 ":rocket:") Run Code ### 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 -* JS String Prototype Split -* JS Array Prototype Shift -* JS Array Prototype Push -* JS Array Prototype Join +* Regular Expressions Reference +* Regular Expressions Resources ### ![:trophy:](https://forum.freecodecamp.com/images/emoji/emoji_one/trophy.png?v=3 ":trophy:") 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`** ## ![:clipboard:](https://forum.freecodecamp.com/images/emoji/emoji_one/clipboard.png?v=3 ":clipboard:") NOTES FOR CONTRIBUTIONS: