Advertisement
depth1

Check nearby

Feb 13th, 2019
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 1.26 KB | None | 0 0
  1. <!doctype HTML>
  2.  
  3. <head>
  4.   <meta charset="utf-8" />
  5. </head>
  6.  
  7. <body>
  8.   <input id="word" type="text" placeholder="mot" value="Bannana">
  9.   <input id="real_word" type="text" placeholder="mot à deviner" value="Banane">
  10.   <button id="check">Check</button>
  11.   <script>
  12.     var dom_word = document.getElementById("word");
  13.     var dom_real_word = document.getElementById("real_word");
  14.     var dom_check = document.getElementById("check");
  15.     dom_check.onclick = function () {
  16.       alert(check_nearly(dom_word.value, dom_real_word.value));
  17.     }
  18.     function check_nearly(word, real_word) {
  19.       if (word == real_word) return 'Bien joué !';
  20.       var regexs = [];
  21.       var i = real_word.length;
  22.       var j = 0;
  23.       while (i--) {
  24.         while (j < real_word.length && j < (i - 1)) {
  25.          var regex = real_word.substr(0, j) + ".{0,2}" + real_word.substr(j + 1, (i - j - 1)) + ".{0,2}" + real_word.substr(i + 1);
  26.          console.log(regex);
  27.          regexs.push(regex);
  28.          j++;
  29.        }
  30.        j = 0;
  31.      }
  32.      for (regex of regexs) {
  33.        console.log(regex);
  34.        var r = new RegExp(regex);
  35.  
  36.        if (word.match(r)) {
  37.          return 'Presque !';
  38.        }
  39.      }
  40.      return 'Pas du tout';
  41.    }
  42.  </script>
  43. </body>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement