From 488c79234a889534869d79aa86b78b44e518d0d0 Mon Sep 17 00:00:00 2001 From: Christopher Willis Date: Wed, 17 Oct 2018 00:54:25 -0500 Subject: [PATCH 1/4] Basic program without comments --- 02week/pigLatin.js | 62 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/02week/pigLatin.js b/02week/pigLatin.js index 046434c94..a08e247b4 100644 --- a/02week/pigLatin.js +++ b/02week/pigLatin.js @@ -8,13 +8,71 @@ const rl = readline.createInterface({ }); +const pigLatinMyString = (word, vowelPosition)=>{ + if(vowelPosition == 0){ + return word + "yay" + } + let mainString = word.split(""); + const firstConsonants = mainString.splice(0,vowelPosition).join(""); + mainString = mainString.join("") + firstConsonants + "ay" + + return mainString; + +} + +const checkValidString = (word,validString)=>{ + for(let i = 0 ; i{ + const allVowels = ["a","e","i","o","u"] + for(let i = 0 ; i { console.log( pigLatin(answer) ); @@ -24,7 +82,7 @@ function getPrompt() { // Tests -if (typeof describe === 'function') { +if(typeof describe === 'function') { describe('#pigLatin()', () => { it('should translate a simple word', () => { From 48013d58ebead47f045a2a0e322ee7c698849015 Mon Sep 17 00:00:00 2001 From: Christopher Willis Date: Sat, 20 Oct 2018 12:12:57 -0500 Subject: [PATCH 2/4] Added comments --- 02week/pigLatin.js | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/02week/pigLatin.js b/02week/pigLatin.js index a08e247b4..18d3b7b97 100644 --- a/02week/pigLatin.js +++ b/02week/pigLatin.js @@ -14,6 +14,7 @@ const pigLatinMyString = (word, vowelPosition)=>{ } let mainString = word.split(""); const firstConsonants = mainString.splice(0,vowelPosition).join(""); + //not sure if there is a single line way to do this, settled on this. Still thinking about it mainString = mainString.join("") + firstConsonants + "ay" return mainString; @@ -22,43 +23,56 @@ const pigLatinMyString = (word, vowelPosition)=>{ const checkValidString = (word,validString)=>{ for(let i = 0 ; i{ const allVowels = ["a","e","i","o","u"] for(let i = 0 ; i Date: Sun, 28 Oct 2018 16:37:08 -0500 Subject: [PATCH 3/4] Changes --- 02week/pigLatin.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/02week/pigLatin.js b/02week/pigLatin.js index 18d3b7b97..f56f3b899 100644 --- a/02week/pigLatin.js +++ b/02week/pigLatin.js @@ -12,19 +12,18 @@ const pigLatinMyString = (word, vowelPosition)=>{ if(vowelPosition == 0){ return word + "yay" } - let mainString = word.split(""); + const mainString = word.split(""); const firstConsonants = mainString.splice(0,vowelPosition).join(""); //not sure if there is a single line way to do this, settled on this. Still thinking about it - mainString = mainString.join("") + firstConsonants + "ay" + return mainString.join("") + firstConsonants + "ay" - return mainString; } const checkValidString = (word,validString)=>{ + let letterIsValid = 0 for(let i = 0 ; i{ } if (!letterIsValid){ //if letter is still invalid the entire string is invalid - return 0; + return letterIsValid; } + letterIsValid = 0 } //if nothing is invalid, then everything is valid - return 1; + letterIsValid = 1; + return letterIsValid; + // implimented your changes, but I have to loop through the entire string, so I have to keep + // the conditional before the return. My logic also assumes the letter is false, so I have + // to reset the isvalid varible each loop. This was done by having it nexted in the loop + // but I took it outside the loop so I could return the value directly. But directly returning + // any of my if statements means only the first letter is checked for validity. } + const findFirstVowelPosition = (word)=>{ const allVowels = ["a","e","i","o","u"] for(let i = 0 ; i Date: Tue, 30 Oct 2018 00:13:56 -0500 Subject: [PATCH 4/4] Refacted and ready for regrade --- 02week/pigLatin.js | 146 ++++++++++++++++++++++++++------------------- 1 file changed, 84 insertions(+), 62 deletions(-) diff --git a/02week/pigLatin.js b/02week/pigLatin.js index f56f3b899..42ca6928d 100644 --- a/02week/pigLatin.js +++ b/02week/pigLatin.js @@ -12,82 +12,104 @@ const pigLatinMyString = (word, vowelPosition)=>{ if(vowelPosition == 0){ return word + "yay" } - const mainString = word.split(""); - const firstConsonants = mainString.splice(0,vowelPosition).join(""); - //not sure if there is a single line way to do this, settled on this. Still thinking about it - return mainString.join("") + firstConsonants + "ay" - - + 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)=>{ - let letterIsValid = 0 - for(let i = 0 ; i { + // 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"] - for(let i = 0 ; i { + // 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