Last active
December 11, 2018 05:42
-
-
Save berkeleycole/cd534044e0c3a5c3df486295c5b5b416 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function pigLatinify() { | |
| // take in words to be translated | |
| var longstring = document.getElementById('input').value | |
| var pigLatin = function(string) { | |
| let newArray = [] | |
| let lowerString = string.toLowerCase() | |
| //split string into individual words | |
| let splitString = lowerString.split(" ") | |
| //run the following on each member of lowerString | |
| splitString.forEach(function(string) { | |
| //create variables for each vowel with the value of that vowel's indexOf | |
| let a = string.indexOf('a') | |
| let e = string.indexOf('e') | |
| let i = string.indexOf('i') | |
| let o = string.indexOf('o') | |
| let u = string.indexOf('u') | |
| //if the first letter is not a y, but the first vowel is a y, include its indexOf | |
| if (string.indexOf('y') != 0) { | |
| var y = string.indexOf('y') | |
| //otherwise assign a -1 to keep it from actualVowels | |
| } else { | |
| var y = -1 | |
| } | |
| let vowels = [a, e, i, o, u, y] | |
| //remove vowels (index) that don't exist | |
| let actualVowels = vowels.filter(vowel => { | |
| return vowel >= 0 | |
| }) | |
| //sort actual vowels (index) | |
| let sortedVowels = actualVowels.sort() | |
| //assign the first vowel's index and the previous index for ease of use in writing logic | |
| let sliceVowel = actualVowels[0] | |
| let qCheck = actualVowels[0] - 1 | |
| //create rules | |
| //if the first vowel is the first letter of the word, just add "way" to the end | |
| if (sliceVowel === 0) { | |
| var transformed = string.concat('way') | |
| } else if (string.charAt(sliceVowel) === 'u' && string.charAt(qCheck) === 'q') { | |
| var transformed = string.slice(sliceVowel + 1).concat(string.slice(0, sliceVowel + 1)).concat('ay') | |
| } else { | |
| var transformed = string.slice(sliceVowel).concat(string.slice(0, sliceVowel)).concat('ay') | |
| } | |
| //make sure punctuation remain at end of words | |
| if (transformed.includes(".")) { | |
| let period = transformed.indexOf(".") | |
| // split the word into an array of letters | |
| let splitWord = transformed.split("") | |
| // use the splice array method to remove period | |
| splitWord.splice(period, 1) | |
| // add a period to the end of the array | |
| let fixedPeriod = splitWord.concat(".") | |
| //join the array | |
| let joinedFix = fixedPeriod.join("") | |
| newArray.push(joinedFix) | |
| } else if (transformed.includes(",")) { | |
| let comma = transformed.indexOf(",") | |
| let splitWord = transformed.split("") | |
| splitWord.splice(comma, 1) | |
| let fixedComma = splitWord.concat(",") | |
| let joinedFix = fixedComma.join("") | |
| newArray.push(joinedFix) | |
| } else if (transformed.includes("?")) { | |
| let question = transformed.indexOf("?") | |
| let splitWord = transformed.split("") | |
| splitWord.splice(question, 1) | |
| let fixedQuestion = splitWord.concat("?") | |
| let joinedFix = fixedQuestion.join("") | |
| newArray.push(joinedFix) | |
| } else if (transformed.includes("!")) { | |
| let exclaim = transformed.indexOf("!") | |
| let splitWord = transformed.split("") | |
| splitWord.splice(exclaim, 1) | |
| let fixedExclaim = splitWord.concat("!") | |
| let joinedFix = fixedExclaim.join("") | |
| newArray.push(joinedFix) | |
| } else { | |
| newArray.push(transformed) | |
| } | |
| }) | |
| // if the first letter is a consonant we wanna push it to a new array. if the first letter of the index, or however many are consonant, push them to a new array, else if its a vowel, then stop. | |
| // | |
| // and then join the two arrays. | |
| // so we need an empty array. and we write the if loop, so that it checks for consonants, if its a consonant, push it | |
| // to the limit. | |
| // cycle thrugh NOT VOWELS until you get to a vowel and then slice the | |
| // console.log(arr[i][0] + " " +vowelReg.test(arr[i][0])) | |
| // console.log(arr[i].includes(vowels[i])) | |
| // function test(arr) { | |
| // let emptyArr = [] | |
| // for(var w=0; w < arr.length; w++) { | |
| // emptyArr.push(arr[w].split("")) | |
| // // for(var l=0, l < arr[w].length; ) | |
| // } | |
| // return emptyArr | |
| // } | |
| //Capitalize the first letter of the supplied string | |
| //split the first word into an array of individual letters | |
| let firstWord = newArray[0].split("") | |
| //replace the first letter with its capital | |
| firstWord[0] = firstWord[0].toUpperCase() | |
| //replace the first word of newArray with its capitalized vesion | |
| newArray[0] = firstWord.join("") | |
| //Check for words that finish with a period and capitalize the next word (see above code) | |
| for (i = 0; i < newArray.length - 1; i++) { | |
| if (newArray[i].includes(".") || newArray[i].includes("?") || newArray[i].includes("!")) { | |
| let letters = newArray[i + 1].split("") | |
| letters[0] = letters[0].toUpperCase() | |
| newArray[i + 1] = letters.join("") | |
| } | |
| } | |
| var joinedString = newArray.join(" ") | |
| var firstCapString = joinedString.charAt(0).toUpperCase() + joinedString.substr(1); | |
| return firstCapString | |
| } | |
| return pigLatin(string) | |
| } | |
| // --------------------------------------------------------------------------------------------------------------------- | |
| // PROBLEMS | |
| // - variable names should be so clear that they don't require comments | |
| // - dead code | |
| // - copy & pasted structurally similar code | |
| // 1. Clean up naming smells | |
| // CHANGE: | |
| // take in words to be translated | |
| var longstring = document.getElementById('input').value | |
| // TO: | |
| var message = document.getElementById('input').value | |
| // no comment required because the name adequately describes what is going on | |
| // ANOTHER EXAMPLE: | |
| // less readable: | |
| // split the word into an array of letters | |
| let splitWord = transformed.split("") | |
| // more readable | |
| let letters = word.split("") | |
| // 2. If you see copy and pasted code that is almost but not quite the same - make a function | |
| if (transformed.includes(".")) { | |
| let period = transformed.indexOf(".") | |
| // split the word into an array of letters | |
| let splitWord = transformed.split("") | |
| // use the splice array method to remove period | |
| splitWord.splice(period, 1) | |
| // add a period to the end of the array | |
| let fixedPeriod = splitWord.concat(".") | |
| //join the array | |
| let joinedFix = fixedPeriod.join("") | |
| newArray.push(joinedFix) | |
| } | |
| // moves punctuation to end of word after letters have been re-arranged | |
| function replacePunctuation(word) { | |
| const punctuation = [".", "!", ",", "?"] | |
| const letters = word.split("") | |
| punctuation.foreach((symbol, index) => { | |
| if(letters.contains(symbol) { | |
| letters.splice(index, 1) | |
| letters.concat(symbol) | |
| } | |
| }) | |
| return letters.join("") | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment