diff --git a/02week/pigLatin.js b/02week/pigLatin.js index 046434c94..42ca6928d 100644 --- a/02week/pigLatin.js +++ b/02week/pigLatin.js @@ -8,13 +8,114 @@ const rl = readline.createInterface({ }); +const pigLatinMyString = (word, vowelPosition)=>{ + if(vowelPosition == 0){ + return word + "yay" + } + return word.split("").slice(vowelPosition).join("") + word.split("").slice(0,vowelPosition).join("") + "ay" + + // REFACTORED + // all the chaining! No more wasted memory, no more mutating. This is a much better solution, albit a little terce +} + +const checkValidString = (word,validString)=>{ + + return word.split("").every(primaryLetter => { + // go through every letter in our Word Array and run this test, return ture if everything passes test + return validString.split("").filter(validatorLetter => validatorLetter == primaryLetter).length>0 + // test creates new array with only matching letters, if you find at least one, we good. + }); + + // REFACTORED + // these 2 lines of code replace allll of this, channeling my inner Katie + // Not only is this code shorter, but it is much easier to read. Every + fliter with some splits means you know you are + // testing every position in one array to see if it is inside another array. You can see it in the symantic words! + // I like this code much better + + // let letterIsValid = 0 + // for(let i = 0 ; i{ + + const allVowels = ["a","e","i","o","u"] + + // REFACTORED + // using what I learned above, I have similar code to finding the first one. + + return word.split("").map(primaryLetter => { + // create new array of only valid vowel positions + return allVowels.filter(validatorLetter => validatorLetter == primaryLetter).length>0 + // match a vowel in word to our allVowels array and throw true in an array + }).indexOf(true) + // look for the first true and there you go + // If this invovled HUGE strings we might reconsider this code as doubling up memory with duplicating it as an array + // might be a poor choices. But for single words, the exchange in memory for readability is a win. + + + // nuked codes, keeping to show others :D + + //for(let i = 0 ; i { console.log( pigLatin(answer) ); @@ -24,7 +125,7 @@ function getPrompt() { // Tests -if (typeof describe === 'function') { +if(typeof describe === 'function') { describe('#pigLatin()', () => { it('should translate a simple word', () => {