From 7a37788ea163300d37cff800dd6efed3bc86551b Mon Sep 17 00:00:00 2001 From: Andrew Ma Date: Fri, 29 Mar 2019 12:18:54 -0500 Subject: [PATCH] add beginner solution to pig latin (#35572) --- .../pig-latin/index.md | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) 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 1b8ff01a65..135d90de37 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 @@ -36,6 +36,31 @@ You will need to use everything you know about string manipulation to get the la **Solution ahead!** +## ![:beginner:](https://forum.freecodecamp.com/images/emoji/emoji_one/beginner.png?v=3 ":beginner:") Basic Code Solution: + + function translatePigLatin(str) { + let consonantRegex = /^[^aeiou]+/; + let myConsonants = str.match(consonantRegex); + return (myConsonants !== null) ? str.replace(consonantRegex, "").concat(myConsonants).concat("ay") : str.concat("way"); + } + + translatePigLatin("consonant"); + + +### Code Explanation: +* start at beginning and get longest match of everything not a vowel (consonants) +* if regex pattern found, it saves the match; else, it returns null + +* if regex pattern found (starts with consonants), it deletes match, adds the match to the end, and adds "ay" to the end +* if regex pattern not found (starts with vowels), it just adds "way" to the ending + +#### Relevant Links + +* Regex Match +* Ternary Operator +* concat() + + ## ![:beginner:](https://forum.freecodecamp.com/images/emoji/emoji_one/beginner.png?v=3 ":beginner:") Basic Code Solution: function translatePigLatin(str) {