Guest User

Untitled

a guest
Nov 24th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.45 KB | None | 0 0
  1. <div class="cover">
  2. <div class="container header">
  3. <div class="row row-margin">
  4. <div class="col-sm-12">
  5. <h1 class="text-center">Palindrome checker</h1>
  6. </div>
  7. <div class="col-sm-12">
  8. <p class="text-center text-uppercase">A nut for a jar of tuna</p>
  9. </div>
  10. </div>
  11. </div>
  12. <div class="container">
  13. <div class="row row-margin">
  14. <div class="col-md-2">
  15.  
  16. </div>
  17. <div class="col-md-8">
  18. <textarea class="col-md-12" id="inputPalindrome" rows="5">Borrow or rob?</textarea>
  19. </div>
  20. </div>
  21. <div class="row row-margin">
  22. <div class="col-sm-3">
  23.  
  24. </div>
  25. <button id="checkPalindrome" class="btn btn-default btn-lg btn-block col-sm-6 col-sm-offset-4" type="submit">Check palindrome</button>
  26. </div>
  27. </div>
  28. <div class="container">
  29. <div class="row row-margin">
  30. <div class="col-md-2">
  31. </div>
  32. <div class="col-md-8 text-center">
  33. <div id="notification" class="alert alert-info">Palindrome has not been checked yet</div>
  34. </div>
  35. </div>
  36. </div>
  37.  
  38. .cover {
  39. height: 100vh;
  40. }
  41.  
  42. .row-margin {
  43. margin: 4vh auto;
  44. }
  45.  
  46. body {
  47. background: #FF4E50;
  48. background: -webkit-linear-gradient(135deg, #FF4E50, #F9D423);
  49. background: linear-gradient(135deg, #FF4E50, #F9D423);
  50. }
  51.  
  52. .header {
  53. color: #fff;
  54. }
  55.  
  56. h1 {
  57. font-family: "Pacifico";
  58. }
  59.  
  60. .btn-default {
  61. background-color: black;
  62. color: #fff;
  63. font-weight: bold;
  64. text-transform: uppercase;
  65. }
  66.  
  67. var checkButton = document.getElementById("checkPalindrome");
  68.  
  69. function isPalindrome(str) {
  70. str = str.toLowerCase().replace(/[^a-z0123456789]+/g,"");
  71. var reversedStr = str.split("").reverse().join("");
  72. if (str == reversedStr) {
  73. return true
  74. }
  75. return false
  76. }
  77.  
  78. checkButton.addEventListener("click", function() {
  79. var palindromeInput = document.getElementById("inputPalindrome").value;
  80. var palindromeReturn = isPalindrome(palindromeInput);
  81. if(palindromeReturn === true) {
  82. document.getElementById("notification").innerHTML = "Yay! You've got yourself a palindrome";
  83. document.getElementById("notification").className = "alert alert-success";
  84. } else {
  85. document.getElementById("notification").innerHTML = "Nay! Ain't no palindrome";
  86. document.getElementById("notification").className = "alert alert-danger";
  87. }
  88. });
  89.  
  90. str.replace(/[^a-z0-9]+/gi, "").toLowerCase();
  91.  
  92. if (str == reversedStr) {
  93. return true
  94. }
  95. return false
  96.  
  97. return str === reversedStr;
  98.  
  99. var notification = document.getElementById('notification');
  100. ...
  101. ...
  102. notification.innerHTML = 'Hello World!';
  103.  
  104. // After DOM is completely loaded
  105. document.addEventListener("DOMContentLoaded", function() {
  106. "use strict";
  107.  
  108. // Cache
  109. var palindromeInput = document.getElementById("inputPalindrome");
  110. var notification = document.getElementById("notification");
  111.  
  112. function isPalindrome(str) {
  113. str = str.replace(/[^a-z0-9]+/gi, "").toLowerCase();
  114. return str.split("").reverse().join("") === str;
  115. }
  116.  
  117. document.getElementById("checkPalindrome")
  118. .addEventListener("click", function() {
  119.  
  120. if (isPalindrome(palindromeInput.value)) {
  121. notification.innerHTML = "Yay! You've got yourself a palindrome";
  122. notification.className = "alert alert-success";
  123. } else {
  124. notification.innerHTML = "Nay! Ain't no palindrome";
  125. notification.className = "alert alert-danger";
  126. }
  127. });
  128. });
  129.  
  130. (function () {
  131. function isPalindrome (str) {
  132. // ...
  133. }
  134.  
  135. document.getElementById("checkPalindrome")
  136. // ...
  137. }());
  138.  
  139. // value is much clear
  140. var value = document.getElementById("inputPalindrome").value;
  141.  
  142. /[^a-z0-9]+/gi
  143. /[^a-z0-9]/gi
  144.  
  145. var value = document.getElementById('inputPalindrome').value,
  146. notification = document.getElementById('notification');
  147.  
  148. .addEventListener("click", function checkPalindrome () {
  149.  
  150. function checkPalindrome () {
  151. 'use strict';
  152. // ...
  153. }
  154.  
  155. document.getElementById('checkPalindrome')
  156. .addEventListener('click', checkPalindrome);
  157.  
  158. (function () {
  159. 'use strict';
  160.  
  161. var value = document.getElementById('inputPalindrome').value,
  162. notification = document.getElementById('notification');
  163.  
  164. function isPalindrome (str) {
  165. str = str.replace(/[^a-z0-9]/gi, '').toLowerCase();
  166. return str.split('').reverse().join('') === str;
  167. }
  168.  
  169. function checkPalindrome () {
  170. if (isPalindrome(value)) {
  171. notification.innerHTML = "Yay! You've got yourself a palindrome";
  172. notification.className = 'alert alert-success';
  173. } else {
  174. notification.innerHTML = "Nay! Ain't no palindrome";
  175. notification.className = 'alert alert-danger';
  176. }
  177. }
  178.  
  179. document.getElementById('checkPalindrome')
  180. .addEventListener('click', checkPalindrome);
  181. }());
  182.  
  183. const arrayFunctionOnString = (s, f) => f(s.split("")).join("");
  184.  
  185. const reverseString = s => arrayFunctionOnString(s, d => d.reverse());
  186.  
  187. const cleanString = s => s.toLowerCase().replace(/[^a-z0-9]/g, "");
  188.  
  189. const isPalindrome = s => s === reverseString(s);
  190.  
  191. const isLoosePalindrome = s => isPalindrome(cleanString(s));
  192.  
  193. const compose = (f, g) => x => f(g(x));
  194.  
  195. const isLoosePalindrome = compose(isPalindrome, cleanString)
  196.  
  197. var variableName = variableName.replace(/s/g, '');
Add Comment
Please, Sign In to add comment