Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. function reverse(str){
  2. let output = ""
  3. for(let i = stirng.length - 1; i >= 0; i--){
  4. output += str[i]
  5. }
  6. return output
  7. }
  8.  
  9. function containsChar(haystack, needle){
  10. for(let i = 0; i < haystack.length; i++){
  11. let currentCharacter = haystack[i]
  12. if(currentCharacter == needle){
  13. return true
  14. }
  15. }
  16. return false
  17. }
  18.  
  19. function indexOfChar(haystack, needle){
  20. for(let i = 0; i < haystack.length; i++){
  21. let currentCharacter = haystack[i]
  22. if(currentCharacter == needle){
  23. return i
  24. }
  25. }
  26. return -1
  27. }
  28.  
  29.  
  30. function removeChar(str, char){
  31. let output = ""
  32. for(let i = 0; i < str.length; i++){
  33. let currentCharacter = str[i]
  34. if(currentCharacter != char){
  35. output += currentCharacter
  36. }
  37. }
  38. return output
  39. }
  40.  
  41. function replaceChar(str, char){
  42. let output = ""
  43. for(let i = 0; i < str.length; i++){
  44. let currentCharacter = str[i]
  45. if(currentCharacter != char){
  46. output += currentCharacter
  47. }else{
  48. output += char
  49. }
  50. }
  51. return output
  52. }
  53.  
  54. function substring(str, start, stop){
  55. let output = "";
  56. for(let i = start; i < stop; i++){
  57. output += str[i]
  58. }
  59. return output
  60. }
  61.  
  62. function contains(haystack, needle){
  63. for(let i = 0; i < haystack.length; i++){
  64. let match = true
  65.  
  66. for(let j = 0; j < needle.length; j++){
  67. let haystackCharacter = haystack[i+j]
  68. let needleCharacter = needle[j]
  69.  
  70. if(haystackCharacter != needleCharacter){
  71. match = false;
  72. break;
  73. }
  74. }
  75.  
  76. if(match){
  77. return true
  78. }
  79. }
  80. return false;
  81. }
  82.  
  83. function contains(haystack, needle){
  84. for(let i = 0; i < haystack.length; i++){
  85. let haystackString = substring(haystack, i, i+needle.length)
  86. if(haystackString == needle){
  87. return true
  88. }
  89. }
  90. return false;
  91. }
  92.  
  93. function remove(haystack, needle){
  94. let output = ""
  95. for(let i = 0; i < haystack.length; i++){
  96. let match = true
  97.  
  98. for(let j = 0; j < needle.length; j++){
  99. let haystackCharacter = haystack[i+j]
  100. let needleCharacter = needle[j]
  101.  
  102. if(haystackCharacter != needleCharacter){
  103. match = false;
  104. break;
  105. }
  106. }
  107.  
  108. if(match){
  109. i += needle.length-1
  110. }else{
  111. output += haystack[i]
  112. }
  113. }
  114. return output
  115. }
  116.  
  117. function remove(haystack, needle){
  118. let output = ""
  119. for(let i = 0; i < haystack.length; i++){
  120. let haystackSubstring = substring(haystack, i, i + needle.length - 1)
  121.  
  122. if(haystackSubstring == needle){
  123. i += needle.length-1
  124. }else{
  125. output += haystack[i]
  126. }
  127. }
  128. return output
  129. }
  130.  
  131. function replace(haystack, needle, replaceWith){
  132. let output = ""
  133. for(let i = 0; i < haystack.length; i++){
  134. let match = true
  135.  
  136. for(let j = 0; j < needle.length; j++){
  137. let haystackCharacter = haystack[i+j]
  138. let needleCharacter = needle[j]
  139.  
  140. if(haystackCharacter != needleCharacter){
  141. match = false;
  142. break;
  143. }
  144. }
  145.  
  146. if(match){
  147. i += needle.length-1
  148. output += replaceWith
  149. }else{
  150. output += haystack[i]
  151. }
  152. }
  153. return output
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement