Guest User

Untitled

a guest
Dec 10th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. function pigLatinify() {
  2. // take in words to be translated
  3. var longstring = document.getElementById('input').value
  4.  
  5. var pigLatin = function(string) {
  6.  
  7. let newArray = []
  8.  
  9. let lowerString = string.toLowerCase()
  10.  
  11. //split string into individual words
  12. let splitString = lowerString.split(" ")
  13.  
  14. //run the following on each member of lowerString
  15. splitString.forEach(function(string) {
  16.  
  17. //create variables for each vowel with the value of that vowel's indexOf
  18. let a = string.indexOf('a')
  19. let e = string.indexOf('e')
  20. let i = string.indexOf('i')
  21. let o = string.indexOf('o')
  22. let u = string.indexOf('u')
  23.  
  24. //if the first letter is not a y, but the first vowel is a y, include its indexOf
  25. if (string.indexOf('y') != 0) {
  26. var y = string.indexOf('y')
  27. //otherwise assign a -1 to keep it from actualVowels
  28. } else {
  29. var y = -1
  30. }
  31.  
  32. let vowels = [a, e, i, o, u, y]
  33.  
  34. //remove vowels (index) that don't exist
  35. let actualVowels = vowels.filter(vowel => {
  36. return vowel >= 0
  37. })
  38.  
  39. //sort actual vowels (index)
  40. let sortedVowels = actualVowels.sort()
  41.  
  42. //assign the first vowel's index and the previous index for ease of use in writing logic
  43. let sliceVowel = actualVowels[0]
  44.  
  45. let qCheck = actualVowels[0] - 1
  46.  
  47. //create rules
  48. //if the first vowel is the first letter of the word, just add "way" to the end
  49. if (sliceVowel === 0) {
  50. var transformed = string.concat('way')
  51. } else if (string.charAt(sliceVowel) === 'u' && string.charAt(qCheck) === 'q') {
  52. var transformed = string.slice(sliceVowel + 1).concat(string.slice(0, sliceVowel + 1)).concat('ay')
  53. } else {
  54. var transformed = string.slice(sliceVowel).concat(string.slice(0, sliceVowel)).concat('ay')
  55. }
  56.  
  57. //make sure punctuation remain at end of words
  58. if (transformed.includes(".")) {
  59. let period = transformed.indexOf(".")
  60. // split the word into an array of letters
  61. let splitWord = transformed.split("")
  62.  
  63. // use the splice array method to remove period
  64. splitWord.splice(period, 1)
  65. // add a period to the end of the array
  66. let fixedPeriod = splitWord.concat(".")
  67.  
  68. //join the array
  69. let joinedFix = fixedPeriod.join("")
  70. newArray.push(joinedFix)
  71. } else if (transformed.includes(",")) {
  72. let comma = transformed.indexOf(",")
  73. let splitWord = transformed.split("")
  74. splitWord.splice(comma, 1)
  75. let fixedComma = splitWord.concat(",")
  76. let joinedFix = fixedComma.join("")
  77. newArray.push(joinedFix)
  78. } else if (transformed.includes("?")) {
  79. let question = transformed.indexOf("?")
  80. let splitWord = transformed.split("")
  81. splitWord.splice(question, 1)
  82. let fixedQuestion = splitWord.concat("?")
  83. let joinedFix = fixedQuestion.join("")
  84. newArray.push(joinedFix)
  85. } else if (transformed.includes("!")) {
  86. let exclaim = transformed.indexOf("!")
  87. let splitWord = transformed.split("")
  88. splitWord.splice(exclaim, 1)
  89. let fixedExclaim = splitWord.concat("!")
  90. let joinedFix = fixedExclaim.join("")
  91. newArray.push(joinedFix)
  92. } else {
  93. newArray.push(transformed)
  94. }
  95. })
  96.  
  97. // 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.
  98. //
  99. // and then join the two arrays.
  100.  
  101. // so we need an empty array. and we write the if loop, so that it checks for consonants, if its a consonant, push it
  102. // to the limit.
  103.  
  104.  
  105. // cycle thrugh NOT VOWELS until you get to a vowel and then slice the
  106.  
  107.  
  108. // console.log(arr[i][0] + " " +vowelReg.test(arr[i][0]))
  109. // console.log(arr[i].includes(vowels[i]))
  110.  
  111.  
  112. // function test(arr) {
  113. // let emptyArr = []
  114. // for(var w=0; w < arr.length; w++) {
  115. // emptyArr.push(arr[w].split(""))
  116. // // for(var l=0, l < arr[w].length; )
  117. // }
  118. // return emptyArr
  119. // }
  120.  
  121.  
  122. //Capitalize the first letter of the supplied string
  123. //split the first word into an array of individual letters
  124. let firstWord = newArray[0].split("")
  125.  
  126. //replace the first letter with its capital
  127. firstWord[0] = firstWord[0].toUpperCase()
  128.  
  129. //replace the first word of newArray with its capitalized vesion
  130. newArray[0] = firstWord.join("")
  131.  
  132. //Check for words that finish with a period and capitalize the next word (see above code)
  133. for (i = 0; i < newArray.length - 1; i++) {
  134. if (newArray[i].includes(".") || newArray[i].includes("?") || newArray[i].includes("!")) {
  135. let letters = newArray[i + 1].split("")
  136. letters[0] = letters[0].toUpperCase()
  137. newArray[i + 1] = letters.join("")
  138. }
  139. }
  140.  
  141. var joinedString = newArray.join(" ")
  142. var firstCapString = joinedString.charAt(0).toUpperCase() + joinedString.substr(1);
  143.  
  144. return firstCapString
  145. }
  146.  
  147. return pigLatin(string)
  148. }
Add Comment
Please, Sign In to add comment