/*
 * Use promise to ensure the words have been loaded before proceeding.
 * https://developers.google.com/web/fundamentals/getting-started/primers/promises
 */
function get(url) {
   // Return a new promise.
   return new Promise(function(resolve, reject) {
     // Do the usual XHR stuff
     var req = new XMLHttpRequest();
     req.open('GET', url);

     req.onload = function() {
       // This is called even on 404 etc
       // so check the status
       if (req.status == 200) {
         // Resolve the promise with the response text
         resolve(req.response);
       }
       else {
         // Otherwise reject with the status text
         // which will hopefully be a meaningful error
         reject(Error(req.statusText));
       }
     };

     // Handle network errors
     req.onerror = function() {
       reject(Error("Network Error"));
     };

     // Make the request
     req.send();
   });
 }

var wordListURL = 'https://davidruffner.github.io/scrabble-word-finder/resources/enable1-wwf-v4.0-wordlist.txt';
var wordListRequest = get(wordListURL)

function findWords() {
    var letters = document.getElementById("letters").value;
    var startsWith = document.getElementById("startsWith").value;
    var endsWith = document.getElementById("endsWith").value;
    var gapLength = document.getElementById("gapLength").value;

    /* Make sure that word list has been loaded */
    wordListRequest.then(function(response) {
      var words = response.split(/\r?\n/);  // Split on newlines
      console.log("Success1!", words[0]);
      var pattern = createPattern(startsWith, endsWith, gapLength);

      console.log('A match? ' + checkWord(pattern, words[0]));
      document.getElementById("demo").innerHTML = letters + startsWith + endsWith + gapLength;
    }, function(error) {
      console.error("Failed!", error);
    });
}

/**
  * Finds regex pattern to pick out words that start with and
  * end with certain letters and have a certain length gap.
  */
function createPattern(startsWith, endsWith, gapLength){
  startPattern = '^' + startsWith;
  endPattern = endsWith + '$';

  if (gapLength != ''){
    console.log('BBB')
    midPattern = '.{' + gapLength + ',' + gapLength + '}';
  }
  else {
    midPattern = '.+';
  }

  var re = new RegExp(startPattern + midPattern + endPattern);
  return re;
}

function checkWord(pattern, word){
  var res = pattern.exec(word);
  if (res == null){
    return false;
  }
  else{
    return res[0];
  }
}

@@ -1568,16 +1568,17 @@
  pattern
+s
  = creat
@@ -1577,32 +1577,33 @@
  = createPattern
+s
 (startsWith, end
@@ -1621,17 +1621,16 @@
 ength);%0A
-%0A
       co
@@ -1643,22 +1643,66 @@
 log(
-'A match? ' + 
+patterns);%0A      console.log(patterns.pattern);%0A      if (
 chec
@@ -1699,32 +1699,41 @@
   if (checkWord(
+patterns.
 pattern, words%5B0
@@ -1735,17 +1735,252 @@
 rds%5B0%5D))
-;
+%7B%0A        console.log('It is a match!');%0A        console.log(split(words%5B0%5D,%0A                          patterns.startPattern,%0A                          patterns.endPattern));%0A      %7D%0A      else %7B%0A        console.log('No match');%0A      %7D
 %0A      d
@@ -2295,16 +2295,17 @@
 ePattern
+s
 (startsW
@@ -2333,16 +2333,20 @@
 gth)%7B%0A  
+var 
 startPat
@@ -2372,16 +2372,20 @@
 With;%0A  
+var 
 endPatte
@@ -2453,24 +2453,28 @@
 ('BBB')%0A    
+var 
 midPattern =
@@ -2533,16 +2533,20 @@
 e %7B%0A    
+var 
 midPatte
@@ -2567,16 +2567,29 @@
 %0A%0A  
-var re =
+return %7B%0A    pattern:
  new
@@ -2639,21 +2639,96 @@
 ern)
-;%0A  return re
+,%0A    startPattern: new RegExp(startPattern),%0A    endPattern: new RegExp(endPattern)%0A  %7D
 ;%0A%7D%0A
@@ -2870,8 +2870,729 @@
 ;%0A  %7D%0A%7D%0A
+%0A/**%0A  * Get none empty splits from a regex pattern applied to a word.%0A  */%0Afunction getNonZeroSplits(pattern, word)%7B%0A  console.log('word' + word);%0A  console.log('pattern' + pattern)%0A  splits = word.split(pattern);%0A  var nonZeroSplits = splits.filter(function(split)%7B%0A    return split != '';%0A  %7D);%0A  console.log(nonZeroSplits);%0A  return nonZeroSplits;%0A%7D%0A%0A/**%0A  * Find leftover fragment after removing characters found by%0A  * startPattern and endPattern.%0A  */%0Afunction split(word, startPattern, endPattern)%7B%0A  end = getNonZeroSplits(startPattern,word)%5B0%5D;%0A  var finalSplits = getNonZeroSplits(endPattern, end);%0A  middle = finalSplits%5BfinalSplits.length - 1%5D;%0A  console.log(middle)%0A  console.log('AAAA')%0A  return middle;%0A%7D%0A

Find the needed letters.